【问题标题】:EF6 Mapping base class properties and derived with different tablesEF6 映射基类属性并使用不同的表派生
【发布时间】:2014-09-23 11:05:43
【问题描述】:

我正在使用 Entity Framework 6,并且我有以下基类

public class ModelBase { public int ModelBaseId { get; set; } public string Name { get; set; } public string Description { get; set; } public IList<Notification> Notifications { get; set; } public IList<Assignable> Viewers { get; set; } }

还有一个派生的

public class Model : ModelBase { public int ModelId { get; set; } public User Owner { get; set; } public DateTime Date { get; set; } public string Observations { get; set; } public User Assignament { get; set; } }

ModelBase 属性填充了所有派生类实例的相同数据。我想用一个表(A)映射基本实体属性,用另一个表(B)映射派生类的属性。当我添加模型时,只需在表 B 中插入。 有没有办法做到这一点?解释起来很复杂,但我试图简化我的问题。

提前致谢!

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    假设您使用的是 Codefirst... 你应该尝试映射它们..

    public class ModelBaseMap:EntityTypeConfiguration<ModelBase>
    {
        public ModelBaseMap()
        {
            this.toTable("ModelBase");
        }
    }
    
    public class ModelMap:EntityTypeConfiguration<Model>
    {
        public ModelBaseMap()
        {
            this.toTable("Model");
        }
    }
    

    我觉得应该可以

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      相关资源
      最近更新 更多