NHibernate & # 39;包# 39;实体框架中的实现

本文关键字:实现 框架 NHibernate 实体 | 更新日期: 2023-09-27 18:05:24

我最近开始在Code First中使用EF,并且遇到了这个让我相当困惑的问题。我将感谢任何关于这个话题的反馈,这将有助于我解决上述问题。

请考虑以下示例....

public class SomeType
{
    public SomeType()
    {
        Properties = new List<BaseProperty>();
    }
    public int PrimaryKey { get; set; }
    public string Name { get; set; }
    public List<BaseProperty> Properties { get; set; }
}
public abstract class BaseProperty
{
    public int PrimaryKey { get; set; } 
    public string PropertyName { get; set; }
    // FK set through Type Configuration File.
    public SomeType ParentInstance { get; set; }
}
public class PropertyA : BaseProperty
{
    // some unique properties.
}
public class PropertyB : BaseProperty
{
    // some unique properties.
}
public class PropertyC : BaseProperty
{
    // some unique properties.
}
public class PropertyD : BaseProperty
{
    // some unique properties.
}

所有这些都适用于映射到2个表的适当类型配置类(1用于'SomeType',第二个用于'BaseProperty',以及通过使用鉴别器列的剩余派生实体)。

现在,由于我无法控制的情况,我被迫修改'SomeType'如下....

public class SomeType
{
    public SomeType()
    {
        PropertiesAB = new List<BaseProperty>();
        PropertiesC = new List<PropertyC>();
        PropertiesD = new List<PropertyD>();
    }
    public int PrimaryKey { get; set; }
    public string Name { get; set; }
    public List<BaseProperty> PropertiesAB { get; set; }        // collection of PropertyA and PropertyB
    public List<PropertyC> PropertiesC { get; set; }        // collection of PropertyC
    public List<PropertyD> PropertiesD { get; set; }        // collection of PropertyD
}

在NHibernate中使用包很容易做到,但是在EF中使用代码优先是否有等价的实现?有什么想法吗?我不想编写自己的Collection实现,因为它将把要在这些新列表上执行的所有操作转发和操作到实际映射到数据库的主列表。

请忽略上面代码中任何缺失的"虚拟"修饰符或其他任何东西,因为它只是一个示例,而不是我实际使用的。

谢谢你的回复。

NHibernate & # 39;包# 39;实体框架中的实现

更糟的是,你可以这样做:

public class SomeType
{
   public SomeType()
   {
      Properties = new List<BaseProperty>();
   }
   public int PrimaryKey { get; set; }
   public string Name { get; set; }
   public List<BaseProperty> Properties { get; set; }
   public List<BaseProperty> PropertiesAB 
   { 
      get
      {
         return Properties.Where(p=>p is PropertyA || p is PropertyB);
      } 
      set
      {
         //Remove all the properties already in the Properties collection of
         //the type A and B and then
         Properties.AddRange(value)
      } 
   }
   //Same with rest of the properties
}

你也可以使Properties属性是内部的,如果类是在域层之外使用