zoukankan      html  css  js  c++  java
  • FluentAPI详细用法

    • 设置主键
      modelBuilder.Entity<x>().HasKey(t => t.Name);
    • 设置联合主键
      modelBuilder.Entity<x>().HasKey(t =>new{t.Name,t.ID} );
    • 取消数据库字段标识(取消自动增长)
      modelBuilder.Entity<x>().Property(t=>t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    • 设置数据库字段标识(自动增长)
      modelBuilder.Entity<Teacher>().Property(t =>t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    • 设置字段最大长度
      modelBuilder.Entity<ClassA>().Property(t => t.Name).HasMaxLength(100);
    • 设置字段为必需
      modelBuilder.Entity<ClassA>().Property(t =>t.Id).IsRequired();
    • 属性不映射到数据库
      modelBuilder.Entity<ClassA>().Ignore(t => t.A);
    • 将属性指定数据库列名:
      modelBuilder.Entity<ClassA>() .Property(t => t.A) .HasColumnName("A_a");
    • 级联删除(数据库默认是不级联删除的)
      modelBuilder.Entity<Course>().HasRequired(t => t.Department).WithMany(t => t.Courses).HasForeignKey(d => d.DepartmentID).WillCascadeOnDelete();
    • 设置为Timestamp
      modelBuilder.Entity<OfficeAssignment>() .Property(t => t.Timestamp) .IsRowVersion();
    • 表1对0..1(Instructor实体可以包含零个或一个OfficeAssignment)
      modelBuilder.Entity<OfficeAssignment>().HasRequired(t => t.Instructor).WithOptional(t => t.OfficeAssignment);
    • 表1对1
      modelBuilder.Entity<Instructor>().HasRequired(t => t.OfficeAssignment).WithRequiredPrincipal(t => t.Instructor);
    • 表1对n(Department为主表)
      modelBuilder.Entity<Staff>() .HasRequired(c => c.Department) .WithMany(t => t.Staffs)
    • 指定外键名(指定表Staff中的字段DepartmentID为外键)
      modelBuilder.Entity<Staff>() .HasRequired(c => c.Department) .WithMany(t => t.Staffs) .Map(m => m.MapKey("DepartmentID"));
    • 表n对n
      modelBuilder.Entity<Course>()
      .HasMany(t => t.Instructors)
      .WithMany(t => t.Courses)
    • 表n对n指定连接表名及列名
      modelBuilder.Entity<Course>()
      .HasMany(t => t.Instructors)
      .WithMany(t => t.Courses)
      .Map(m =>
      {
      m.ToTable("CourseInstructor");
      m.MapLeftKey("CourseID");
      m.MapRightKey("InstructorID");
      });



  • 相关阅读:
    8u111-jdk-alpine 字体缺少FontConfiguration的NullPointerException错误解决方案
    Mybatis插件原理
    Mybaits 分页
    @requestBody 和@RequestParam
    Mybaits 生产mapper
    powerDesigner 生成SQL时没有注释问题
    HashMap 的 put过程
    Java的锁
    Java1.8 JVM配置 GC日志输出
    Windows 安装两个MYSQL实例
  • 原文地址:https://www.cnblogs.com/giftmm/p/9308676.html
Copyright © 2011-2022 走看看