Mapping 2 tables to single entity in Entity Framework

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


I hope you can help me.

I have 2 tables in the db: Bill and BillItem. These tables are configured with one to one relation in the db where bill is the principle table while BillItem is the dependent one

The structure of table Bill :

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

The structure of table BillItem:

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

I would like to map these 2 table into a single POCO class using Fluent API and Entity Framework 4.1 Code First approach

I also want to configure the tables columns names to use different properties names in the POCO Class (i.e. Id instead of BillId, User instead of Usr)

This is a legacy database, I cannot modify any of its objects.

How can achieve that?

Thank you all.

The resulting class should be (if can 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

for this question you can do it very easily and you got 2 options. The first is to map the classes using entity framework (mapping multiple tables to a single entity class in entity framework).

the second option is to do it manually: The restriction the 2 tables must follow is that 1 has to be the strong table and the second needs to be weak one (by doing this, you should have 1 by one only from the strong table to the weak) otherwise, this wont work as the result will be a collection of objects and then you will need to decide whether if the first object is the one that you need and no other.

if you want to use a slightly faster approach i reccomend using your own logic to map the POCO objects. create 2 clases that will map with the database table and from a service object build the object.

this approach is good because the resulting LINQ query wont use INNER JOINS which makes it faster. but the join should be kept from 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;

This approach is faster but you need to control everything from yourself. the good thing is that here you can map as many tables as you want

hope it helps!

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