【问题标题】:Entity Framework 6: Two unrelated entities mapped to same table实体框架 6:映射到同一张表的两个不相关实体
【发布时间】:2018-12-04 09:16:17
【问题描述】:

我想将一个表映射到两个不相关的实体:EntityBasic 和 EntityAdvanced。 EntityAdvanced 有额外的业务逻辑,我不需要这个功能,我想创建一个只包含表中字段的新实体。

我的表:

MyTableId : Guid
ParentId : Guid
Name : string
Description : string
Type : int

实体基础:

[Table("MyTable")]
public class EntityBasic
{
    [Key]
    public Guid MyTableId { get; set; }

    public Guid ParentId { get; set }

    public string Name { get; set; }

    public string Description { get; set; }

    public int Type { get; set; }

    [ForeignKey("ParentId")]
    public virtual List<EntityBasic> Entities{ get; set; }
}

EntityAdvanced:

[Table("MyTable")]
public class EntityAdvanced
{
    private List<EntityAdvanced> _entities;
    private List<Filter> _filters;

    [Key]
    public Guid MyTableId { get; set; }

    public Guid ParentId { get; set }

    public string Name { get; set; }

    public string Description { get; set; }

    public int Type { get; set; }

    [ForeignKey("ParentId")]
    public virtual List<EntityAdvanced> Entities
    {
        get { //Some complicated getter }
        set { //Some complicated setter }
    }

    [NotMapped]
    public string ImageUrl
    {
        get { //Some complicated getter }
        set { //Some complicated setter }
    }

    public void SetFilters(//Some parameters)
    {
        //Some logic 
    }
}

当我这样做时,我得到了这个错误:

实体类型“EntityAdvanced”和“EntityBasic”不能共享表“MyTable”,因为它们不在同一类型层次结构中,或者它们之间没有有效的一对一外键关系和匹配的主键。

有没有办法做我想做的事?

【问题讨论】:

  • 使EntityAdvanced 继承并建立在EntityBasic 之上。这将满足“相同类型的层次结构”

标签: c# entity-framework-6


【解决方案1】:

首先,您的EntityAdvanced 应该继承 EntityBasic,因为它们共享相同的基本属性集。你不需要重写它们。注意extends EntityBasic

[Table("MyTable")]
public class EntityBasic
{
    [Key]
    public Guid MyTableId { get; set; }

    public Guid ParentId { get; set }

    public string Name { get; set; }

    public string Description { get; set; }

    public int Type { get; set; }

    [ForeignKey("ParentId")]
    public virtual List<EntityBasic> Entities{ get; set; }
}

[NotMapped]
public class EntityAdvanced : EntityBasic
{    
    //[NotMapped]
    public string ImageUrl
    {
        get { //Some complicated getter }
        set { //Some complicated setter }
    }

    public void SetFilters(//Some parameters)
    {
        //Some logic 
    }
}

使用继承,List&lt;EntityBasic&gt; Entities 可以引用EntityAdvanced 对象,因此您不再需要声明:

[ForeignKey("ParentId")]
public virtual List<EntityAdvanced> Entities
{
    get { //Some complicated getter }
    set { //Some complicated setter }
}

您可以获得有关使用实体框架here 实现继承的有用信息。

编码愉快!

【讨论】:

  • 当我这样做时,我得到这个错误:无效的列名'鉴别器'。
  • 查看编辑。修复很简单,你应该在子类中添加[NotMapped] 属性
  • 应用程序的某些部分使用了 EntityAdvanced,如果我将 NotMapped 添加到它,正如预期的那样,会发生这种情况:“类型 'EntityAdvanced' 未映射。检查该类型是否未明确排除通过使用 Ignore 方法或 NotMappedAttribute 数据注释。验证该类型被定义为一个类,不是原始的或泛型的,并且不是从 EntityObject 继承的。还有其他选择吗?还是我必须找到其他解决方案?
【解决方案2】:

我认为你可以使用实体框架6的“表拆分”能力, 看看这里的例子:https://www.c-sharpcorner.com/UploadFile/ff2f08/table-splitting-in-entity-framework-6-code-first-approach/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2012-09-29
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    相关资源
    最近更新 更多