zoukankan      html  css  js  c++  java
  • EF Fluent API

    EntityTypeConfiguration

    它可以为一个实体类,配置一个模型。

    Entity Framework 6 一次加载许多个 Fluent API 映射

    如果对于许多实体有大量映射,则 OnModelCreating 中会有许多重复的 modelBuilder.Configurations.Add 方法。 为了从这种枯燥的工作中解放出来,您现在可以只用一个方法从给定程序集加载所有 EntityTypeConfiguration。 这里,我使用新的 AddFromAssembly 方法来加载在正在运行的应用程序的执行程序集中指定的配置:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除复数表名的契约     
                modelBuilder.Conventions.Remove<IncludeMetadataConvention>();//防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
     
                modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
            }
    

    代表从这句话所在的程序集,加载所有的继承自EntityTypeConfiguration为模型配置类。

    这种方法的一个出色功能是,它不受将要加载的配置的范围限制。 甚至可以将自定义 EntityTypeConfiguration 类标记为私有,该方法会找到这些类。 此外,AddFromAssembly 也理解 EntityTypeConfiguration 中的继承层次结构。

    一对多

    和关系映射相关的方法:
    1) 基本套路this.Has****(p=>p.A).With***() 当前这个表和A属性的表的关系是Has定义,
    With定义的是A对应的表和这个表的关系。Optional/Required/Many
    2) HasOptional() 有一个可选的(可以为空的)
    3) HasRequired() 有一个必须的(不能为空的)
    4) HasMany() 有很多的
    5) WithOptional() 可选的
    6) WithRequired() 必须的
    7) WithMany() 很多的

    实体关系

    总结一对多、多对多的“最佳实践”

    一对多最佳方法(不配置一端的集合属性):

    1、 多端

        public class Student 
        { 
            public long Id { get; set; } 
            public string Name { get; set; } 
            public long ClassId { get; set; } 
            public virtual Class Class { get; set; } 
        } 
    

    2、 一端

        public class Class 
        { 
            public long Id { get; set; } 
            public string Name { get; set; } 
        } 
    

    3、 在多端的模型配置(StudentConfig)中:

    this.HasRequired(e => e.Class).WithMany() .HasForeignKey(e=>e.ClassId); 
    

    一对多的配置(在一端配置一个集合属性,极端不推荐)

    1、多端

        public class Student 
        { 
            public long Id { get; set; } 
            public string Name { get; set; } 
            public long ClassId { get; set; } 
            public virtual Class Class { get; set; } 
        } 
    

    2、 一端

        public class Class 
        { 
            public long Id { get; set; } 
            public string Name { get; set; } 
            public virtual ICollection<Student> Students { get; set; } = new List<Student>(); 
        } 
    

    3、 多端的配置(StudentConfig)中

    this.HasRequired(e => e.Class).WithMany(e=>e.Students).HasForeignKey(e=>e.ClassId); 
    //WithMany()的参数不能丢  
    

    多对多最佳配置

    1、 两端模型

        public class Student 
        { 
            public long Id { get; set; } 
            public string Name { get; set; } 
            public virtual ICollection<Teacher> Teachers { get; set; } = new List<Teacher>(); 
        } 
    	public class Teacher 
    	{ 
    		public long Id { get; set; } 
    		public string Name { get; set; } 
    		public virtual ICollection<Student> Students { get; set; } = new List<Student>(); 
        } 
    

    2、 在其中一端配置(StudentConfig)

        this.HasMany(e => e.Teachers).WithMany(e=>e.Students).Map(m =>  //不要忘了WithMany的参数 
    m.ToTable("T_StudentTeachers").MapLeftKey("StudentId").MapRightKey("TeacherId")); 
    

    4、 多对多中 移除关系:t.Students.Remove(t.Students.First()); 添加关系
    5、 (*)多对多中还可以为中间表建立一个实体方式映射。当然如果中间关系表还想有其他字
    段,则要必须为中间表建立实体类(中间表和两个表之间就是两个一对多的关系了)。

  • 相关阅读:
    Vue 使用百度地图 实现搜索 定位
    VUE npm run dev 启动时,报了一大堆错误 Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 7.x
    git 更换push 提交地址
    vue 拖拽框架 draggable
    VUE axios请求 封装 get post Http
    关于git 远程仓库账户密码错误的问题
    输入交互(一)
    8.实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署
    7.kubernetes集群版本升级
    6.kubernetes的GUI资源管理插件-dashboard
  • 原文地址:https://www.cnblogs.com/tangge/p/9831957.html
Copyright © 2011-2022 走看看