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();
            }
  • 相关阅读:
    java学习笔记——基于Robot类的屏幕分享
    Java实例——基于jsoup的简单爬虫实现(从智联获取工作信息)
    Java实例练习——基于UDP协议的多客户端通信
    java实例练习——基于TCP/IP协议的多客户端通信
    我个人的Java学习经验(一家之言)
    PHP mac localhost 环境下发送邮件
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    程序员应该知道的13个设计技巧
    程序员应该知道的13个设计技巧
  • 原文地址:https://www.cnblogs.com/cuishengli/p/4724579.html
Copyright © 2011-2022 走看看