zoukankan      html  css  js  c++  java
  • 【译】第24节---Fluent API

    原文:http://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api.aspx

    本节,我们将学习如何使用Fluent API配置实体类的属性。 我们将使用我们学校app的Student和Standard域类:

    public class Student
    {
        public 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 Standard()
        { 
            
        }
        public int StandardKey { get; set; }
        public string StandardName { get; set; }
        
        public ICollection<Student> Students { get; set; }
    }

    配置主键和复合主键

    上面的域类,没有按照主键的Code-First约定,因为它们没有Id或{Class Name} + Id属性。

    因此,你可以使用Fluent API使用EntityTypeConfiguration的HasKey()方法配置key属性,如下所示。 记住modelBuilder.Entity <TEntity>()返回EntityTypeConfiguration对象。

    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 }); 
        }
    }

    配置列名、列类型和排序

    默认Code-First约定为名属性创建一个同名的列名、顺序和数据类型的列。 你可以覆盖此约定,如下所示:

    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 Column
            modelBuilder.Entity<Student>()
                        .Property(p => p.DateOfBirth)
                        .HasColumnName("DoB")
                        .HasColumnOrder(3)
                        .HasColumnType("datetime2");
        }
    }

    如上例所示,我们使用Property()方法为实体的属性配置任何东西。

    在这里,我们使用HasColumnName来更改DateOfBirth属性的列名。 此外,我们调用HasColumnOrder和HasColumnType来更改列的顺序和数据类型。

    modelBuilder.Entity <TEntity>().Property(expression)允许您使用不同的方法配置特定属性,如下所示:

    为属性配置Null或Not Null列

    Code-First将为原始数据类型属性创建NotNull列,因为原始数据类型不能为空,除非使用?符号或Nullable <T>。

    使用IsOptional方法为属性创建一个可空的列。

    以同样的方式,使用IsRequired方法创建一个NotNull列。

    namespace CodeFirst_FluentAPI_Tutorials
    {
            
        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 Null Column
                modelBuilder.Entity<Student>()
                        .Property(p => p.Heigth)
                        .IsOptional();
                            
                    //Configure NotNull Column
                    modelBuilder.Entity<Student>()
                        .Property(p => p.Weight)
                        .IsRequired();
            }
        }
    }

    配置列大小

    Code-First将设置列的数据类型的最大大小。 您可以覆盖此约定,如下所示:

    namespace CodeFirst_FluentAPI_Tutorials
    {
            
        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)
            {
                //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);
            }
        }
    }

    如上例所示,我们使用HasMaxLength方法设置列的大小。

    IsFixedLength方法将nvarchar转换为nchar类型。

    以同样的方式,HasPrecision方法改变了十进制列的精度。

    配置并发列

    你可以使用ConcurrencyToken方法将属性配置为并发列,如下所示:

    namespace CodeFirst_FluentAPI_Tutorials
    {
            
        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)
            {
                //Set StudentName as concurrency column
                modelBuilder.Entity<Student>()
                        .Property(p => p.StudentName)
                        .IsConcurrencyToken();
            }
        }
    }

    如上例所示,我们将StudentName列设置为并发列,以便它被包含在update和delete命令的where子句中。

    您也可以使用IsRowVersion()方法来将byte []属性作为并发列。

  • 相关阅读:
    Beta阶段团队项目开发篇章2
    Beta阶段团队项目开发篇章1
    Alpha阶段博客链接
    大学生对技术网站需求的调查问卷结果分析
    关于大学生对技术网站需求的问卷调查
    第十。。。三周嘞
    进入第十一周喽
    第十周了耶~
    第九周也要过去啦~~~
    第,八,周,啦
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7268551.html
Copyright © 2011-2022 走看看