zoukankan      html  css  js  c++  java
  • Code First:Fluent API

    DbContext类有一个OnModelCreating方法,可以在这里配置模型,该方法接收一个类型为DbModelBuilder的建造者,本文介绍的为Data Anotation的等价方法,这些代码是自解释的。

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                // Configure Code First to ignore PluralizingTableName convention
                // If you keep this convention then the generated tables will have pluralized names.
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    
                // Default Schema (EF6 onwards)
                modelBuilder.HasDefaultSchema("sales");
    
                // Configuring a Primary Key [Key]
                modelBuilder.Entity<OfficeAssignment>().HasKey(t => t.InstructorID);
    
                // Configuring a Composite Primary Key
                modelBuilder.Entity<Passport>().HasKey(t => new { t.PassportNumber, t.IssuingCountry });
    
                // Switching off Identity for Numeric Primary Keys
                modelBuilder.Entity<Passport>().Property(t => t.PassportNumber).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                // Specifying the Maximum Length on a Property
                modelBuilder.Entity<Passport>().Property(t => t.IssuingCountry).HasMaxLength(50);
    
                // Configuring the Property to be Required
                // (NOT NULL) [Required]
                modelBuilder.Entity<Passport>().Property(t => t.Issued).IsRequired();
    
                // Specifying Not to Map a CLR Property to a Column in the Database
                // [NotMapped]
                modelBuilder.Entity<Blog>().Ignore(t => t.BlogCode);
    
                // Mapping a CLR Property to a Specific Column in the Database
                // [Column("BlogTitle")]
                modelBuilder.Entity<Blog>().Property(t => t.Title).HasColumnName("BlogTitle");
    
                // Configuring whether a String Property Supports Unicode Content
                // (varchar not nvarchar)
                modelBuilder.Entity<Blog>().Property(t => t.Title).IsUnicode(false);
    
                // Configuring the Data Type of a Database Column [Column(TypeName = "varchar")]
                modelBuilder.Entity<Department>().Property(p => p.Name).HasColumnType("varchar");
    
                // Mapping an Entity Type to a Specific Table in the Database
                modelBuilder.Entity<Department>().ToTable("t_Department");
    
                // 第二个参数是SCHEMA
                modelBuilder.Entity<Department>().ToTable("t_Department", "school");
    
                // Specifying That a Class Is a Complex Type
                modelBuilder.ComplexType<Details>();
    
                // Specifying Not to Map a CLR Entity Type to a Table in the Database
                // [NotMapped]
                modelBuilder.Ignore<OnlineCourse>();
    
                // Configuring Properties on a Complex Type
                // Method 1
                modelBuilder.ComplexType<Details>().Property(t => t.Location).HasMaxLength(20);
                // Method 2
                modelBuilder.Entity<OnsiteCourse>().Property(t => t.Details.Location).HasMaxLength(20);
    
                // Configuring a Property to Be Used as an Optimistic Concurrency Token
                modelBuilder.Entity<OfficeAssignment>().Property(t => t.Timestamp).IsConcurrencyToken();
    
                // Configuring the property to be a row version in the database
                modelBuilder.Entity<OfficeAssignment>().Property(t => t.Timestamp).IsRowVersion();
            }
  • 相关阅读:
    关于C语言变量初始化的两个问题的笔记
    源自StackOverflow:找到LIST中第一个降序成员,有助于对扩展方法、IEnumerable<T>、泛型的理解
    《PERL高效编程》学习笔记(1)【关于裸字处理】
    C#出题库项目的总结(2)
    遇到的浏览器兼容问题及应对方法
    春招面试小记
    关于团队成员的退出引发的一些回忆及感悟
    C#出题库项目的总结(1)
    写一个程序,分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来
    重定向和管道符
  • 原文地址:https://www.cnblogs.com/cuishengli/p/4724579.html
Copyright © 2011-2022 走看看