FluentAPI详细用法

2021-06-29 04:05

阅读:323

标签:指定   ipa   require   time   on()   col   class   详细   ade   

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

FluentAPI详细用法

标签:指定   ipa   require   time   on()   col   class   详细   ade   

原文地址:https://www.cnblogs.com/wangshaod/p/10025836.html


评论


亲,登录后才可以留言!