We are trying to use EF
as a Business Entity Mapping model
. Instead of generating 1 table per class we would like to flatten out the tables (entity splitting) and generate a business object for the class, letting Entity Framework handle the CRUD.
In addition we have an existing database and cannot modify the schema. We have tried both code-first and database first solutions, but we are unable to get to our stated goal.
What we have:
public class Call
{
public int CallId {get;set;}
public DateTime callTime {get;set;}
public int PhoneTypeId {get; set;}
public virtual PhoneType PhoneType {get; set;}
}
public class PhoneType
{
public int PhoneTypeId {get ;set;}
public string TypeOfPhone {get ;set;}
}
What we want:
public class CallDetails
{
public int CallId {get;set;}
public DateTime callTime {get;set;}
public string TypeOfPhone {get; set;}
}
Edit
We are on ef 5 but can go up to ef 6.
Edit
To state the question clearer, I am looking for a way to flatten out multiple tables into 1 single entity, then have EF automagically update the multiple tables when that one edit is saved back to the data context
Edit
Is there a way of mapping 1 entity to second, if the first entity has the primary key of the second and the second entity doesnt have any key relating back to the first?
If you don't like how EF builds the class for you, create your own just as you want it. (I wrote the following assuming you have C# 6)
public class CallDetails
{
public Call entityCall { get; set;}
public int CallId => entityCall.CallId;
public DateTime callTime {
get{ return entityCall.callTime;}
set{ entityCall.callTime = value;}
}
public string TypeOfPhone => entityCall.PhoneType.TypeOfPhone;
}
Then when you use it
var details = new CallDetails(){ entityCall = db.Calls.Find(id) };
You can access the values in the structure that you set up.That being said, I'd suggest using EF's classes. Doing it this way can cause a headache when it comes to editing values in the database.