zoukankan      html  css  js  c++  java
  • 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);//设置非自增列

    参考

    作者:chenze
    出处:https://www.cnblogs.com/chenze-Index/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文中有什么错误,欢迎指出。以免更多的人被误导。
  • 相关阅读:
    NSLayoutConstraint 开源框架
    NSLayoutConstraint 开源框架
    IOS6 新特性之UIRefreshControl
    IOS6 新特性之UIRefreshControl
    IOS6 新特性之UIActivityViewController详解
    Openstack组件部署 — Overview和前期环境准备
    红帽虚拟化RHEV3.2创建虚拟机(图文Step by Step)
    红帽虚拟化RHEV3.2创建虚拟机(图文Step by Step)
    Oracle 表空间详解
    Oracle 表空间详解
  • 原文地址:https://www.cnblogs.com/chenze-Index/p/9711239.html
Copyright © 2011-2022 走看看