Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have multiple entities mapped against one table?

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!

like image 714
Stian Avatar asked Sep 04 '13 14:09

Stian


1 Answers

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.

like image 80
Paddy Avatar answered Oct 29 '22 13:10

Paddy