10

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.

1 Answer 1

5

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.

1
  • Good, It validates the model and I can see my entity in a domain service class but it says no definition for this.ObjectContext.MyClass , So I couldn't find a way to manage this for SL and for a view. The designer class looks just like the other entities except inheriting from BaseClass instead of Entity... Year 2014 :) Jul 24, 2014 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.