删除外键属性导致异常
我不想使用与 CompanyType 的外键关联(将保存外键 id 的成员),但更喜欢使用导航属性。所以我删除了 CompanyTypeId。
我得到一个与实体 Company 和 CompanyType 之间的关系相关的异常:
错误5:元素“Principal”位于 名称空间 'http://schemas.microsoft.com/ado/2008/09/edm' 内容不完整。名单 预期可能的要素: 命名空间中的“PropertyRef” 'http://schemas.microsoft.com/ado/2008/09/edm'.
如何从 POCO 中删除这些 id 而不会出现异常?
I don't want to use foreign key association to CompanyType (member that will hold the foreign key id) but prefer to use navigation property. So I removed the CompanyTypeId.
I get this exception that relates the relationship between entity Company and CompanyType:
Error 5: The element 'Principal' in
namespace
'http://schemas.microsoft.com/ado/2008/09/edm'
has incomplete content. List of
possible elements expected:
'PropertyRef' in namespace
'http://schemas.microsoft.com/ado/2008/09/edm'.
How can I remove those id's from the POCOs without getting the exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是外键关联和独立关联之间的区别< /a>.两个关联都使用导航属性,但只有外键关联也使用 FK 属性。您可以像 @Robbie 提到的那样全局删除它们,也可以手动更改所选关系的类型。
这是我的一个测试应用程序的屏幕截图,其中
Order
和之间存在一对多关系>OrderLine
实体:如您所见,没有
OrderId
在OrderLine
实体中,关系的引用约束为空。还指定了关系的映射。但是您永远无法从
CompanyType
中删除Id
。 Id (PK) 是强制性的。您只能在其属性中更改其可访问性。This is the difference between Foreign key association and Independent association. Both associations use navigation properties but only Foreign key association uses FK property as well. You can either remove them globally as @Robbie mentioned or you can change the type manually for selected relation.
Here is the screen shot from one of my testing application with one-to-many relation between
Order
andOrderLine
entities:As you can see there is no
OrderId
in theOrderLine
entity and referential constraints of the relation are empty. Also mapping of the relation is specified.BUT you can't never remove
Id
fromCompanyType
. Ids (PKs) are mandatory. You can only change its accessibility in its properties.当您从数据库导入模型时,系统会询问您是否要:
“在模型中包含外键列”
你需要把它关掉。
When you imported in your Model from your DB you are asked if you want to:
"Include Foreign key columns in the model"
you need to switch this off.