We’re trying to implement a data layer with ORM between our domain entities and our tables using EF. We’ve successfully done trivial mappings with domain entities that closely resemble their respective tables, but have stumbled when trying to map objects with more complex relationships that include foreign keys in their table representations. I’ve found plenty of references to “Table Splitting” using EF with tables that share primary keys, but that’s not the case with our schema.
As an example, I’d like to have domain entities as POCO’s with no more awareness of the data layer than simple ID’s:
public class EntityInfo
{
public int EntityId { get; set; }
public string EntityName { get; set; }
public string TypeName { get; set; }
public string ComponentName { get; set; }
}
public class Entity
{
public int EntityId { get; set; }
public EntityType Type { get; set; }
public EntitySource Source { get; set; }
public string Name { get; set; }
}
public class EntityType
{
public int TypeId { get; set; }
public string Name { get; set; }
}
public class EntitySource
{
public int SourceId { get; set; }
public string Name { get; set; }
}
These would be mapped to a series of equivalent tables with foreign keys:
Table Entities:
[PK] int EntityId
[FK] int TypeId
[FK] int SourceId
string Name
Table EntityTypes:
[PK] int TypeId
string Name
Table EntitySources:
[PK] int SourceId
string Name
What we’d really like to do is separate the table representation from the domain entities entirely and implement the mapping layer (.msl) to resolve these somewhat complex relationships. The EntityInfo class above would be built from and store to the tables, despite having no direct table representation of its own. Is such a mapping (multiple tables-to-object hierarchy) even possible using an .msl? Is it possible with EF?
Did you at least try it? There is no complex mapping in your presented model. That is the most basic mapping of one-to-many relationships. Simply open EDMX in the designer and run Update from database command (from context menu). Select your three tables in the wizard and uncheck option for adding foreign keys to the model.
Edit:
Yes entity framework is able to map even your
EntityInfoobject but the object will be read only because all information about what related entities (their FK relations) it includes will be lost. There are two ways to map it:DefiningQueryis custom SQL used to load columns you want to map. EF doesn’t bother how many tables you join to get your result set. The problem is that default designer will delete yourDefiningQueryeach time you try to Update model from database.QueryViewis custom Entity SQL view mapped to a new enttiy. It is built on entities defined in your model. So you must mapEntity,EntityTypeandEntitySourceas well (but you can make them for example internal to your data assembly).QueryViewsurvives updates from database.Both
DefiningQueryandQueryViewmust be defined directly in EDMX (opened as XML) because designer doesn’t support them. These features are not available in code-first approach. These features create read only entities so if you want CUD operations as well you have to create stored procedures and map them to newly created entities.There is also one last option most commonly used – you can create linq-to-entities query simply projecting your mapped entities to unmapped
EntityInfo: