在EF4.1 +中,您是否必须在从属实体上公开外键属性以强制执行标识关系? [英] In EF4.1+, do you have to expose a foreign key property on a dependent entity to enforce an identifying relationship?

查看:110
本文介绍了在EF4.1 +中,您是否必须在从属实体上公开外键属性以强制执行标识关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用EF 4.1+(或EF5 Beta)创建标识关系,这是否意味着我必须在从属实体上公开外键属性?使用Fluent API时?当DbContext与Entity不在同一个项目中时?

If I want to create an identifying relationship using EF 4.1+ (or EF5 Beta), does this mean I have to expose a foreign key property on the dependent entity? When using the Fluent API? And when the DbContext is in a different project than the Entity?

我看不到其他任何方法. DbModelBuilder需要知道外键属性,以便将其作为主键的一部分包含在内.但是,如果未公开该属性,则实体& DbContext实现在不同的项目/库中,无法公开(不想将InternalsVisibleTo放在实体库上).

I can't see any other way to do it. The DbModelBuilder needs to know the foreign key property in order to include it as part of the primary key. However if the property is not exposed, and the Entity & DbContext implementations are in different projects/libraries, there is no way to expose it (don't want to put InternalsVisibleTo on the entity lib).

推荐答案

识别关系始终需要在从属实体上公开FK属性,因为该属性必须是主键的一部分-这就是识别关系的原因.我没有用DbContext尝试过它,但是除非存在一些内部问题/错误,否则它应该可以工作.

Identifying relationship always needs FK property exposed on the dependent entity because the property must be part of primary key - that is what makes it identifying. I didn't try it with DbContext but unless there is some internal issue / bug it should work.

这篇关于在EF4.1 +中,您是否必须在从属实体上公开外键属性以强制执行标识关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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