How to have multiple entities mapped against one table?

asp.net-mvc c# entity-framework

Question

I'm trying to use two different entities against the same table. The purpose of having two entities is to limit the amount of attributes in one of them, because on one of the edit forms it should only be possible to change a few of the attributes.

So in order to avoid having to have the non-editable attributes as hidden to preserve their values, I thought having a separate entity with just a portion of the attributes would be a good idea.

So I have one entity with all the attributes, and one with just some of the attributes. The problem is that I get this exception:

`The entity types 'ApplicationMapping' and 'ApplicationMappingFull' cannot share table 'ApplicationMapping' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.

The entity config classes look like this:

class ApplicationMappingFullConfiguration : EntityTypeConfiguration<ApplicationMappingFull>  
{  
  public ApplicationMappingFullConfiguration()  
  {  
    ToTable("ApplicationMapping");  
    HasKey(p => p.Id);  
  }  
}  

class ApplicationMappingConfiguration : EntityTypeConfiguration<ApplicationMapping>  
{  
  public ApplicationMappingConfiguration()  
  {  
    ToTable("ApplicationMapping");  
    HasKey(p => p.Id);  
  }  
}

How can I achieve what I'm trying to do? Is there a better/simpler way of doing it?

Thanks!

1
10
9/4/2013 2:12:03 PM

Accepted Answer

I would recommend having a single entity mapped to the table, but create two 'view' entities that contain only those properties that the form requires.

These view entities could contain methods to map data entered back to the underlying entity.

4
9/4/2013 2:14:00 PM

Popular Answer

It seems what you're looking for is TPH (Table Per Hierarchy). This is an relational inheritance pattern where a class and all it's subclasses share the same table and is supported natively by Entity Framework. In fact, if all you do is have one entity inherit from another, Entity Framework deploys this pattern by default. You don't need to do anything special at all.

However, there's a condition: since the properties on the base class must be enough on their own to be able to successfully save the row to the database, all your subclass properties must be optional -- at least on a database level. You could always enforce one or more properties to be required through the frontend user interface.

UPDATE

Expanding on my comment below, if all you're looking to do is return a subset of data from the table, then you already have all the tools you need. You don't need two separate entities, just one entity (in this case, your ApplicationMappingFull class) and then you can use LINQ to return only those columns you need.

db.ApplicationMappingFulls.Select(m => new ApplicationMappingViewModel
    {
        SomeProperty = m.SomeProperty,
        OtherProperty = m.OtherProperty
    });

Behind the scenes, EF will issue a query that will only select the SomeProperty and OtherProperty columns, because that's all that's needed. Your view model would not be connected to EF at all; it's just a class to hold the data returned by EF.



Related Questions





Related

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