Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entities framework mapping association between view and table

I can map 1:1 (one-to-one) tables intuitively, like this:

But I cannot understand how to do the same mapping between a table and a VIEW, like this

In this diagram the two entities are represented. If I manually create an association in the entity model, and set up its mapping like this:

Then I get the error:

Error 3021: Problem in Mapping Fragment starting at line 172: Each of the following columns in table view_EmployeeView is mapped to multiple conceptual side properties: view_EmployeeView.EmployeeID is mapped to Employeesview_EmployeeView.Employees.id, Employeesview_EmployeeView.view_EmployeeView.EmployeeID

Why would I not get this error with the table-table association? How do I solve this problem? I would like to put some calculated information in a view, but explicitly join to it when I need with the .Include() function.

like image 666
tenfour Avatar asked Jul 26 '10 15:07

tenfour


People also ask

What is mapping in Entity Framework?

Entity Framework is an Object Relational Mapper (ORM) which is a type of tool that simplifies mapping between objects in your software to the tables and columns of a relational database. Entity Framework (EF) is an open source ORM framework for ADO.NET which is a part of . NET Framework.

What is Navigation property in Entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name. (Required)

What are the types of Entity Framework?

There are two types of Entities in Entity Framework: POCO Entities and Dynamic Proxy Entities.


1 Answers

To map an association between two entities, the foreign key can not also be the primary key.

What you really have here is a TPT inheritance. You have a "base" class, plus optional additional properties in a second table (or view).

watch this video: http://msdn.microsoft.com/en-us/data/cc765425.aspx

Make the "view" entity inherit from the Employee entity. Remove the EmployeeID property from the view entity. Map the EmployeeID column of the View to the ID property of the base Employee. You will get a single ObjectSet in your ObjectContext for this hierarchy.

like image 73
Ray Henry Avatar answered Oct 09 '22 03:10

Ray Henry