视图和表之间的实体框架映射协会 [英] Entities framework mapping association between view and table

查看:116
本文介绍了视图和表之间的实体框架映射协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以映射1: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:

然后我得到的错误:

错误3021:问题在映射片段起始于行172:每个表view_EmployeeView下面列映射到多个概念端属性:
view_EmployeeView.EmployeeID映射到Employeesview_EmployeeView.Employees.id,Employeesview_EmployeeView.view_EmployeeView .EmployeeID

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

我为什么不得到这个错误与表表关联?我该如何解决这个问题?我希望把一些计算的信息视图,但明确地加入到它时,我需要在.include()函数。

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.

推荐答案

要映射两个实体之间的关联,外键不能同时是主键。

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

你真正在这里是一个TPT的继承。你必须在第二个表(或视图),一个基地类,加上可选的附加属性。

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

观看此视频:的 http://msdn.microsoft.com/en-us/data/cc765425.aspx

请在视图实体从雇员实体继承。取下视图实体雇员财产。地图的视图到基础员工的ID属性的EmployeeID列。你会得到一个对象集在你的ObjectContext这个层次。

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.

这篇关于视图和表之间的实体框架映射协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆