zoukankan      html  css  js  c++  java
  • EF Code-First 学习之旅 EntityTypeConfiguration<TEntity>

    之前我们配置的实体都都在OnModelCreating方法中,如果有很多实体的话,OnModelCreating方法管理很麻烦

    我们可以用单独的类来管理配置,继承EntityTypeConfiguration<TEntity>类实现

    复制代码
    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");
                            });
        }
    }
         
    复制代码
    复制代码
    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());
                   
        }
    }
  • 相关阅读:
    js中有趣的闭包(closure)
    js常见函数汇总
    js时间处理
    一些JavaScript的技巧、秘诀和最佳实践
    js创建对象的6种方式
    js数组常用方法汇总
    左右点击分页方法
    dedecms 添加自定义图字段,调用时出错
    js判断PC端与移动端跳转
    php验证码
  • 原文地址:https://www.cnblogs.com/sjqq/p/7448300.html
Copyright © 2011-2022 走看看