简体   繁体   English

实体框架映射视图和表之间的关联

[英]Entities framework mapping association between view and table

I can map 1:1 (one-to-one) tables intuitively, like this: 我可以直观地映射1:1(一对一)表,如下所示:

But I cannot understand how to do the same mapping between a table and a VIEW, like this 但我无法理解如何在表和VIEW之间进行相同的映射,就像这样

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 错误3021:从第172行开始映射片段中的问题:表view_EmployeeView中的以下每列都映射到多个概念侧属性:view_EmployeeView.EmployeeID映射到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. 我想在视图中放置一些计算信息,但在我需要.Include()函数时显式加入它。

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. 你真正拥有的是TPT继承。 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 观看此视频: http//msdn.microsoft.com/en-us/data/cc765425.aspx

Make the "view" entity inherit from the Employee entity. 使“视图”实体继承自Employee实体。 Remove the EmployeeID property from the view entity. 从视图实体中删除EmployeeID属性。 Map the EmployeeID column of the View to the ID property of the base Employee. 将View的EmployeeID列映射到基本Employee的ID属性。 You will get a single ObjectSet in your ObjectContext for this hierarchy. 您将在ObjectContext中为此层次结构获取单个ObjectSet。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM