zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(16):Move Configurations

    Move Configurations to Separate Class in Code-First:

    By now, we have configured all the domain classes in OnModelCreating method in the previous sections. When you have a large number of domain classes, then configuring every class in OnModelCreating can become unmanageable. Code-First enables you to move all the configurations related to one domain class to a separate class.

    In the below example, we configured Student entity.

    public class SchoolDBContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }
    
        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        public DbSet<StudentAddress> StudentAddress { get; set; }
            
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
                modelBuilder.Entity<Student>().ToTable("StudentInfo");
                    
                modelBuilder.Entity<Student>().HasKey<int>(s => s.StudentKey);
                    
                modelBuilder.Entity<Student>()
                        .Property(p => p.DateOfBirth)
                        .HasColumnName("DoB")
                        .HasColumnOrder(3)
                        .HasColumnType("datetime2");
    
                modelBuilder.Entity<Student>()
                        .Property(p => p.StudentName)
                        .HasMaxLength(50);
                            
                    modelBuilder.Entity<Student>()
                        .Property(p => p.StudentName)
                        .IsConcurrencyToken();
                    
                modelBuilder.Entity<Student>()
                    .HasMany<Course>(s => s.Courses)
                    .WithMany(c => c.Students)
                    .Map(cs =>
                            {
                                cs.MapLeftKey("StudentId");
                                cs.MapRightKey("CourseId");
                                cs.ToTable("StudentCourse");
                            });
        }
    }

    Now, you can move all the configurations related to Student entity to a separate class which derives from EntityTypeConfiguration<TEntity>. Consider the following StudentEntityConfigurations class.

    public class StudentEntityConfiguration: EntityTypeConfiguration<Student>
    {
        public StudentEntityConfiguration()
        {
            
                this.ToTable("StudentInfo");
                    
                this.HasKey<int>(s => s.StudentKey);
                    
                    
                this.Property(p => p.DateOfBirth)
                        .HasColumnName("DoB")
                        .HasColumnOrder(3)
                        .HasColumnType("datetime2");
    
                this.Property(p => p.StudentName)
                        .HasMaxLength(50);
                            
                this.Property(p => p.StudentName)
                        .IsConcurrencyToken();
                    
                this.HasMany<Course>(s => s.Courses)
                    .WithMany(c => c.Students)
                    .Map(cs =>
                            {
                                cs.MapLeftKey("StudentId");
                                cs.MapRightKey("CourseId");
                                cs.ToTable("StudentCourse");
                            });
        }
    }

    As you can see above, we have moved all the configuration for the Student entity into constructor of StudentEntityConfiguration, which is derived from EntityTypeConfiguration<Student>. You need to specify entity type in a generic place holder for which you include configurations, Student in this case.

    Now, you can inform Fluent API about this class, as shown below.

    public class SchoolDBContext: DbContext 
    {
        public SchoolDBContext(): base() 
        {
        }
    
        public DbSet<Student> Students { get; set; }
        public DbSet<Standard> Standards { get; set; }
        public DbSet<StudentAddress> StudentAddress { get; set; }
            
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
                // Moved all Student related configuration to StudentEntityConfiguration class
                modelBuilder.Configurations.Add(new StudentEntityConfiguration());
                   
        }
    }

    Thus, you can use a separate class to configure a domain class to increase the readability and maintainability.

  • 相关阅读:
    和类的初识
    IIFE
    闭包的二次理解
    5分钟搞懂ECE雾计算
    机智云推出设备联动API,打造智能家居场景化
    神奇的【设备联动API】,助力智能家居/家电场景化
    STM32峰会2017:半小时开发基于STM32的室内智能环境监测仪
    STM32峰会:机智云MCU代码开发工具降低智能硬件开发成本
    广和通G510联网固件首发,支持连接机智云
    各种STM32连接机智云案例汇总(不定期更新,欢迎补充)
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644338.html
Copyright © 2011-2022 走看看