【问题标题】:Entity Framework Base Class实体框架基类
【发布时间】:2016-03-29 09:30:53
【问题描述】:

拥有实体对象的基类我面临循环引用问题和基类依赖角色引入问题,只要我有实体的复合类:

public abstract class Base
{
   public int Id {get;set;}
   public DateTime CreationTime {get;set;}
   public User Creator {get; ste;}
}
public class User : Base
{
   public string Name {get;set;}
   public Country BirthPlace {get;set;}
   // rest of the properties
}
public class Country : Base
{
   public string Description {get;set;}
   //rest of the properties
}

知道如何克服这个问题吗?

【问题讨论】:

  • 您好,如果我的解决方案回答了您的问题,请将问题标记为有用,谢谢 :)
  • 谢谢队友做到了:)

标签: entity-framework superclass circular-reference


【解决方案1】:

创建一个映射类以忽略基类用户

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        Ignore(x => x.User);
    }
}

从基础中删除用户并将其添加到需要它的每个域中(示例)

public class Country : Base
{
   public string Description {get;set;}
   public User Creator {get; set}
   //rest of the properties
}

【讨论】:

    最近更新 更多