在实体框架中的SQL视图上使用外键 [英] Use Foreign Key on SQL View in Entity Framework

查看:245
本文介绍了在实体框架中的SQL视图上使用外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Entity Framework相对较新。



我创建了一个包含两个表的数据库:帐户作业



帐户有一个 AccountId 主键,用作属性表中的外键。我真的很喜欢Entity Framework自动获取外键关系,并允许我从作为每个行的属性 Accounts 中的行访问



我继续创建了一个新的View对象,它从帐户以及其他一些信息。但是,当我使用实体框架从SQL中查看数据时,它不再自动引用作业表中的关联行。



如何使用实体框架视图获取所需的行为

解决方案

这可以在EF,但是EF设计师不能将FK推出视图,因为DB不会在视图列上显示FKs(因为它们自然位于底层表,而不是视图本身)。



您需要手动编辑您的EDMX,无论是通过设计者还是使用XML,以获取这些属性


I'm relatively new to Entity Framework.

I created a database with two tables: Accounts and Assignments.

Accounts has an AccountId primary key which is used as a foreign key in the Properties table. I really like that the Entity Framework automatically picks up the foreign key relationship and allows me to access rows in Assignments as a property of each row from Accounts.

I went ahead and created a new View object that returns all the columns from Accounts along with some other information. However, when I get the data from the View in SQL using the Entity Framework, it is no longer automagically referencing the associated rows in the Assignments table.

How can I get the desired behavior using Views with Entity Framework

解决方案

This can work in EF, but the EF designer can't infer your FK out of the view, since the DB doesn't tell it where the FKs on view columns are (since they're naturally on the underlying tables, not the view itself).

You'll need to manually edit your EDMX, either via the designer or in XML, to get these properties.

这篇关于在实体框架中的SQL视图上使用外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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