1

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?

gh9
  • 10,169
  • 10
  • 63
  • 96
  • what exactly is your question? – MakePeaceGreatAgain Feb 16 '16 at 21:04
  • What version of Entity Framework are you working with? – Dave Feb 16 '16 at 21:05
  • @HimBromBeere updated with exact question – gh9 Feb 16 '16 at 21:08
  • @dave updated with ef version – gh9 Feb 16 '16 at 21:09
  • Can you give more details about how entity splitting didn't work? This article appears to describe how to do exactly what you're asking: https://msdn.microsoft.com/en-us/data/jj715646.aspx – Dave Feb 16 '16 at 21:12
  • Show us the code you tried in Code First ? – CodeNotFound Feb 16 '16 at 21:18
  • You title says *entity splitting one to many*, which is a contradictio in terminis. But the example is 1:1. So what do you mean? Maybe showing the database schema could shed some light on this. – Gert Arnold Feb 16 '16 at 21:18
  • @GertArnold The example is 1 - Many if you look at it from the phonetype. There is 1 phonetype with many phones – gh9 Feb 16 '16 at 21:19
  • OK, so, no entity splitting here. – Gert Arnold Feb 16 '16 at 21:20
  • You basically look for a way to have children copy some values from their parents. EF doesn't provide any mapping strategy for that. – Gert Arnold Feb 16 '16 at 21:23
  • @GertArnold that is exactly what I want to do – gh9 Feb 16 '16 at 21:25
  • @GertArnold The only thing that comes close to what I want, is that both entites Must share the same key. Can we create a mapping where 1 entity has the key of the second, but the second entity doesnt have the key of the first? – gh9 Feb 16 '16 at 21:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103649/discussion-between-gh9-and-gert-arnold). – gh9 Feb 16 '16 at 21:31

1 Answers1

2

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.

Blue Eyed Behemoth
  • 3,692
  • 1
  • 17
  • 27