Let's go

CodeFrist基础_Fluent Api

一丶首先新建两个实体类

public class Student
{
    public int StudentKey { get; set; }
    public string StudentName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public byte[]  Photo { get; set; }
    public decimal Height { get; set; }
    public float Weight { get; set; }
        
    public Standard Standard { get; set; }
}
    
public class Standard
{
    public int StandardKey { get; set; }
    public string StandardName { get; set; }
    
    public ICollection<Student> Students { get; set; }
}

二丶设置主键和复合主键(实体配置均在重写虚方法【OnModelCreating】里面)

public class SchoolContext: DbContext 
{
    public SchoolDBContext(): base() 
    {
    }

    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
        
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Configure primary key[配置主键]
        modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
        modelBuilder.Entity<Standard>().HasKey<int>(s => s.StandardKey);

        //Configure composite primary key【配置复合主键】
        modelBuilder.Entity<Student>().HasKey<int>(s => new { s.StudentKey, s.StudentName }); 
    }
}

三丶配置列名称、类型和顺序

 modelBuilder.Entity<Student>()
                    .Property(p => p.DateOfBirth)
                    .HasColumnName("DoB")
                    .HasColumnOrder(3)
                    .HasColumnType("datetime2");

四丶配置不为空和为空的情况

            //Configure Null Column
        modelBuilder.Entity<Student>()
                .Property(p => p.Heigth)
                .IsOptional();
                        
            //Configure NotNull Column
        modelBuilder.Entity<Student>()
                .Property(p => p.Weight)
                .IsRequired();

五丶配置列的大小

     //Set StudentName column size to 50
        modelBuilder.Entity<Student>()
                .Property(p => p.StudentName)
                .HasMaxLength(50);
                        
        //Set StudentName column size to 50 and change datatype to nchar 
        //IsFixedLength() change datatype from nvarchar to nchar
        modelBuilder.Entity<Student>()
                .Property(p => p.StudentName)
                .HasMaxLength(50).IsFixedLength();
                        
        //Set size decimal(2,2)
        modelBuilder.Entity<Student>()
                .Property(p => p.Height)
                .HasPrecision(2, 2);

六丶配置并发列(乐观锁)

        //Set StudentName as concurrency column
        modelBuilder.Entity<Student>()
                .Property(p => p.StudentName)
                .IsConcurrencyToken();    

正如您在上面的示例中看到的,我们将StudentName列设置为并发列,以便将其包括在update和delete命令中的where子句中。

还可以使用IsRowVersion()方法来将字节[]类型属性作为并发列。

 this.Property(p => p.TimeStramp).IsRowVersion(); 

 

七丶将学生实体映射到数据库中的多个表。

            modelBuilder.Entity<Student>().Map(m =>
            {
                m.Properties(p => new { p.StudentId, p.StudentName});
                m.ToTable("StudentInfo");
            }).Map(m => {
                m.Properties(p => new { p.StudentId, p.Height, p.Weight, p.Photo, p.DateOfBirth});
                m.ToTable("StudentInfoDetail");
            });

            modelBuilder.Entity<Standard>().ToTable("StandardInfo");    

八丶设置自增列

 modelBuilder.Entity<Student>.Property(s => s.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);//设置自增列

 modelBuilder.Entity<Student>.Property(s => s.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);//设置非自增列

 

 

参考

posted @ 2018-09-27 08:58  chenze  阅读(172)  评论(0编辑  收藏  举报
有事您Q我