Fluent API

https://www.e-learn.cn/content/net/921278

 

 

    • Set the primary key
      modelBuilder.Entity <x> () HasKey ( t => t.Name).;
    • Provided the primary key
      modelBuilder.Entity <x> () HasKey ( t => new {t.Name, t.ID}).;
    • Identifying a database field cancellation (cancel the automatic increase)
      modelBuilder.Entity <X> () Property (T => t.Id) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.None).;
    • Setting identification database field (automatic growth)
      modelBuilder.Entity <Teacher> () Property (T => t.Id) .HasDatabaseGeneratedOption (DatabaseGeneratedOption.Identity).;
    • The maximum length setting field
      modelBuilder.Entity <ClassA> () Property ( t => t.Name) .HasMaxLength (100).;
    • Setting field is required
      . ModelBuilder.Entity <ClassA> () Property (t => t.Id) .IsRequired ();
    • Attribute is not mapped to the database
      modelBuilder.Entity <ClassA> () Ignore ( t => tA).;
    • Property specifies the name of the database column:
      modelBuilder.Entity <of ClassA> () .Property (T => tA of) .HasColumnName ( "A_A");
    • Cascading deletes (default database is not cascading deletes)
      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();
    • Table 1 0..1 (Instructor entity may contain zero or one 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);
    • Table 1 n (Department primary table)
      modelBuilder.Entity <Staff> () .HasRequired (C => c.Department) .WithMany (T => t.Staffs)
    • Specifies the name of the foreign key (Staff fields in the specified table to the foreign key 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");
      });

Guess you like

Origin www.cnblogs.com/laodu0/p/11111565.html