Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Suppose I have an existing set of database tables as following.

1. Medication_Orders
2. Lab_And_Radiology_Orders
3. Immunization_Orders

You can see that these tables relates to a concept of "Orders". Each table its own entity data except Lab_And_Radiology which holds two distinct type of Orders. They have a discriminator column called Order_Type to different between between Lab and Radiology orders.

In Code First, I defined my class hierarchy as following.

<pre lang="c#">
public abstract class Order { //base class properties }

public class LabOrder:Order { //populates from table # 2 above where order_type = 'L' }

public class RadiologyOrder:Order { //populates from table # 2 above where order type='R'}

public class MedicationOrder:Order {//populates from table# 1 above.}

public class ImmunizationOrder:Order { //populates from table# 3 above.}

I'm not able to configure this in E.F code first fluent API. As you can see my tables have both TPH and TPC scenarios.

What I have tried:

I have tried to implement this in my sample app but unable to make it work.
Posted
Updated 26-May-16 14:15pm
Comments
Sergey Alexandrovich Kryukov 26-May-16 18:16pm    
All right, you can do this, but "unable to make it work" is not informative. Perhaps you need to know what "What I have tried" means. This

From your post, it's hard to understand what kind of help would you possibly mean.

—SA

1 solution

First you haven't shown us your DbContext and your configuration, so it's pretty much impossible to tell you where you went wrong. But, by what you have posted, i think you're over-complicating things.

Next, all of these types will end up in the same table, Orders. Your discriminator for Lab and Radiology reports is redundant.

C#
public abstract class Order

public class LabOrder : Order

public class MedicationOrder : Order

public class ImmunizationOrder : Order

DbContext:
C#
public class MyDbContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
}

That's it! Nothing else needs to be done for a basic table-per-hierarchy inheritance graph.

If this is your first foray into Entity Framework, I HIGHLY suggest you pickup a book or two on it before you dive in. You're not going to learn all of the in's and out's and conventions that EF uses from a bunch of forum posts.
 
Share this answer
 
v3
Comments
i_syed2000 27-May-16 10:42am    
Thanks Dave. I'm sorry if I wasn't clear enough. You have Implemented DB Context correctly. However, I want to mention that table structure is existing. I will not be able to change it. Is it possible make this class hierarchy work with the table structure I have mentioned.
Dave Kreskowiak 27-May-16 11:45am    
It's possible, but using Code First to do it this way is not going to be fun. You have to configure everything to match the existing database by hand and you better get it right! Failure to do so will result in screwy things happening to the database and possible hosing up your data integrity.

A better choice would be to use Database First instead. That way you get all the relationships discovered and done for you.
i_syed2000 27-May-16 12:02pm    
Also there is no relationship defined between tables in the Database. Do you think it is possible to map that table structure to the domain model structure using the fluent API?
Dave Kreskowiak 27-May-16 12:16pm    
No relationships? Yeah, that's doesn't sound like a good thing. That sounds like a small database that really isn't doing much of anything.

It's possible and probably just as easy as I've shown. Truthfully, I'd probably scrap the database and create a new one, importing the data from the old database.
i_syed2000 27-May-16 12:31pm    
Its really big DB. I think foreign key constraints gets in the way of high volume transactional applications. But that may be another debate.

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



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