Mapping 2 tables to single entity in Entity Framework

c# code-first ef-code-first entity-framework fluent-interface


Hopefully you can assist me.

Bill and BillItem are two tables I have in the database. These tables are set up in the database with a one-to-one relationship, with the primary table being bill and the dependent table being BillItem.

The Bill table's format:

Int BillId (PK)
Int BillTypeId Not Null
Varchar(5) Usr Not Null
DateTime Tm Not Null

The design of the table BillItem:

Int BillItemId (PK)
Int ItemId Not Null 
Varchar(5) Usr Not Null
DateTime Tm Not Null

Using the Fluent API and Entity Framework 4.1 Code First approach, I would like to map these two tables into a single POCO class.

Additionally, I want to change the table's column names to reflect certain POCO Class features (i.e. Id instead of BillId, User instead of Usr)

Because it is an old database, I am unable to change any of its objects.

How can that be done?

Thank to everybody.

If possible, the generated class should be:

public int Id {get;set;}
public int BillTypeId {get;set;}
public int ItemId {get;set;}
public string User {get;set;}
public string User1 {get;set;}
public DateTime Tm {get;set;}
public DateTime Tm1 {get;set;}
3/4/2013 2:14:15 PM

Popular Answer

You have two options for this question, and it is extremely simple to answer. The initial step is to use entity framework to map the classes (Entity Framework's mapping of several tables to a single entity class).

The second choice is to carry it out by hand: The constraint that the two tables must adhere to is that one must be the strong table and the other must be the weak one (by doing this, you should have 1 by one only from the strong table to the weak) Otherwise, this won't work since you'll end up with a collection of things and have to decide whether the initial object is the only one you require.

Use your own logic to map the POCO objects if you wish to take a somewhat quicker method. Create two classes that correspond with the database table, then construct the object from a service object.

This strategy is advantageous since the final LINQ query will not employ INNER JOINS, making it faster. But you should keep the joint on your side.


int i=0;
// this 2 refer to the 2 different mapped tables in your context database
var pbdata = _pbdata.GetSingle(p=>p.StrongTableID == StrongTableID);
var pb = _pb.GetSingle(p=>p.WeakTableID == pbdata.WeakTableID);
// you can see that i am looking for the StrongTableID in order to select the entity value

// this is optional but usefull! you can do the
// "copy" inside a function where in future if the 
// object changes, you can update easily
ObjectComposite.Map(body, ref pb, ref pbdata);

// the following proccess needs to be done like this...
// first save the value in the weak object so 
// the WeakTableID is recorded
// UPDATE: maybe these 2 should go into a transact for more security... but... ok...
i += this.unitOfWork.Save();
pbdata.ProfessionalBodyID = pb.ProfessionalBodyID;

// once the values for the second objects are set, save it all again.
i += this.unitOfWork.Save();
return i > 0 ? true : false;

Although it is quicker, you must exercise total self-control. The advantage is that you can map as many tables as you'd like here.

Hope it's useful!

5/23/2017 12:23:44 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow