views:

46

answers:

1

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.

A: 

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.

Ray Henry