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

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

问题描述

我可以直观地映射1:1(一对一)表,如下所示:

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

但是我不明白如何在表和VIEW之间进行相同的映射,像这样

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

使视图实体从Employee实体继承。从视图实体中删除EmployeeID属性。将View的EmployeeID列映射到基本Employee的ID属性。您将在该层次结构的ObjectContext中获取一个ObjectSet。

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天全站免登陆