NHibernate“包”实体框架中的实现

发布于 2024-11-29 07:40:54 字数 1865 浏览 3 评论 0原文

我最近开始将 EF 与 Code First 结合使用,并遇到了这个让我相当困惑的问题。我将不胜感激有关此主题的任何反馈,这将有助于我解决上述问题。

请考虑以下示例......

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 个表的适当类型配置类配合得很好(第一个用于“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 中使用 Code 是否有等效的实现第一的 ?有什么想法吗? 我不想编写自己的集合实现,它将转发和操作要在这些新列表上执行的所有操作到主列表,该主列表将实际映射到数据库。

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

谢谢您的回复。

I started using EF with Code First recently and have come upon this issue which has left me rather perplexed. I will appreciate any feedback on this topic which will help me in resolving the said issue.

Please consider the following sample....

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.
}

All of this works great with the appropriate type configuration classes which map to 2 tables (1 for 'SomeType' and the second for 'BaseProperty' along with the remaining derived entities through the use of a discriminator column).

Now, due to circumstances beyond my control, I am being forced to modify 'SomeType' to something like this....

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
}

This would be very fairly easy to do in NHibernate using bags but is there an equivalent implimentation for this in EF using Code First ? Any thoughts ?
I do not want to write my own implimentation of a Collection which will forward and manipulate all operations to be performed on these new lists to a master list which will be actually mapped to the database.

Please ignore any missing "virtual" modifiers or anything else in the above code since it is only meant to be a sample and is NOT actually what I am using.

Thank you for your replies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-12-06 07:40:54

更糟糕的是,您可以执行以下操作:

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 属性设置为内部属性

Worse comes to Worse, you can do something like this:

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
}

You can also make the Properties property internal if the class is being used outside the domain layer

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文