Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Apr-14 10:30am    
The problem is clearly formulated, and this is the problem of mapping between OOP model and relational model. My 5.
—SA
Pheonyx 11-Apr-14 10:38am    
Thanks :-)
Guruprasad.K.Basavaraju 11-Apr-14 12:36pm    
So what I understand is the Single Market is a single instance of the class and Combined Market is a collection, correct ?
Pheonyx 11-Apr-14 14:35pm    
Hi Guruprasad,

Not quite, another way to explain it is this:
A single market is say England,
A combined market is say United Kingdom, which has a property on it called "ChildMarkets", for example this could contain England, Scotland, Wales.

A combined market could also be Europe, which could have France, Germany (single markets), and United Kingdom (a combined market).

Does that make more sense?


Have a look here: Ignoring a class property in Entity Framework 4.1 Code First[^]

I hope that might help.
 
Share this answer
 
v2
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 :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900