2011-08-11 186 views
3

我最近开始在Code First使用EF,并且遇到了这个让我感到困惑的问题。我会很感激任何关于这个主题的反馈,这将有助于我解决上述问题。NHibernate'Bags'在实体框架中的实现

请考虑下面的示例....

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的做,但在使用Code First的EF中是否存在等同的暗示?有什么想法吗 ? 我不想写我自己的集合的implimentation,它将转发和操作这些新列表上执行的所有操作,并将其实际映射到数据库的主列表。

请忽略任何缺少的“虚拟”修饰符或上述代码中的任何其他内容,因为它只是一个示例,实际上并不是我正在使用的。

谢谢你的回复。

+0

以外使用的是什么'BasePropertyC '和'BasePropertyD'? –

+0

哦对不起。 'BasePropertyC'和'BasePropertyD'应该分别是'PropertyC'和'PropertyD'。我的错。 – James

+0

“PropertiesAB”呢?我可以在'PropertiesAB'中存储'PropertyD'(我可以,因为'PropertiesAB'是'BaseProperty'的集合,'PropertyD'是'BaseProperty'的扩展]? –

回答

0
更糟

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

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 
} 

您也可以使属性属性内部类是否正在域层