导航属性的对象继承 - 实体框架数据库优先 [英] Object Inheritance for Navigation Properties - Entity Framework Database First

查看:92
本文介绍了导航属性的对象继承 - 实体框架数据库优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我遇到了Entity Framework和对象继承问题。



对象概念



基类 - 市场

派生类 - SingleMarket / CombinedMarket



两者SingleMarket和CombinedMarket应基于市场类别。但是我在数据库中只有一个市场表。数据库表有一个名为类型的列,其中我存储了一个指示符,表明这是一个单一/组合市场。到目前为止,这个概念很有用。



现在我的问题



组合市场由2个或更多单个市场组成,实质上它是一个组

我创建了一个名为Market_Market的表,它有两列,ParentMarketID和 ChildMarketId,这为基本形式的市场建立了多对多的关系。



从对象的角度来看,我想拥有一个SingleMarket对象和CombinedMarket对象一样,CombinedMarket对象应该有一个导航属性来链接它ChildMarkets但是我不知道如何将这个导航属性从基础Market类移动到派生的CombinedMarket。 />


我不希望在SingleMarket上提供该物业。




我希望我已经清楚地解释了这一点,你可以理解我的问题,如果不是问问题,我会尽力澄清。



谢谢。

Hi guys,

I am having an issue with Entity Framework and object inheritance.

Object Concept:

Base class - Market
Derived Classes - SingleMarket / CombinedMarket

Both of the SingleMarket and CombinedMarket should be based on the Market Class. However I only have one "Market" table in the database. The Database table has a column called "Type" which in it I store an indicator to say if this is a Single/Combined Market. So far so good, this concept works.

Now my issue,

The Combined market is made up of 2 or more single markets, in essence it is a "group"
I have created a table called "Market_Market" which has two columns, "ParentMarketID" and "ChildMarketId", this estabilishes a "many to many" relationship for the markets in the base form.

From an object perspective, I want to have a "SingleMarket" object and a "CombinedMarket" object, the CombinedMarket object should have a navigation property on it to link it "ChildMarkets" but I'm not sure how to move this navigation property from the base Market class to the derived CombinedMarket.

I don't want the property to be available on the SingleMarket.


I hope I've explained this clearly enough for you to understand my issue, if not ask questions and I will try and clarify.

Thanks.

推荐答案

看看这里:忽略实体框架4.1代码优先的类属性[ ^ ]



我希望可能有帮助。
Have a look here: Ignoring a class property in Entity Framework 4.1 Code First[^]

I hope that might help.


对象继承建模对EF来说是一场噩梦^ ^



按原样,你只有一个'市场'数据库中的表;您不能强制执行Market_Market表中ParentMarketId链接到市场表中类型列表示合并市场的记录的约束。因此,由于您无法在数据库中强制执行此约束,因此您无法使用EF对其进行建模。



您可以尝试为每个对象创建一个表,抽象与否。因此:

- 一个'Market'表将映射到你的抽象基类

- 一个'SingleMarket'表,其主键将被约束为外键关系使用'市场'表的主键

- 一个'CombinedMarket'表,其主键将被限制为与'市场'表的主键的外键关系

- 一个' Market_Market'表,其'ParentMarketId'列将被限制为与'CombinedMarket'表的主键的外键关系,其'ChildMarketId'列将被限制为与'SingleMarket'表的主键的外键关系



...并在刷新模型后看看EF处理的内容。



我完全清楚它但是,这不是一个完整的解决方案;正如我先说的那样,使用EF的继承建模给我带来了很多麻烦,我现在不惜一切代价避免它。



希望这仍有帮助。祝你好运:)
Object inheritance modeling is quite a nightmare with EF ^^

As is, you only have one 'Market' table in your database; you cannot enforce the constraint that ParentMarketId in 'Market_Market' table links to a record in the 'Market' table whose 'Type' columns states a combined market. Thus, as you can't enforce this constraint in the database, you cannot model it with EF, either.

You may try to create one table for each object, abstract or not. Thus:
- one 'Market' table which will be mapped to your abstract base class
- one 'SingleMarket' table whose primary key will be constrained to a foreign key relationship with 'Market' table's primary key
- one 'CombinedMarket' table whose primary key will be constrained to a foreign key relationship with 'Market' table's primary key
- one 'Market_Market' table, whose 'ParentMarketId' column will be constrained to a foreign key relationship with 'CombinedMarket' table's primary key, and whose 'ChildMarketId' column will be constrained to a foreign key relationship with 'SingleMarket' table's primary key

... and see what EF deals with that after refreshing your model.

I'm totally aware that it is not a full solution, though; as I said first, inheritance modeling with EF used to give me so much troubles that I now try to avoid it at all costs.

Hope this helps, still. Good luck :)


这篇关于导航属性的对象继承 - 实体框架数据库优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆