简体   繁体   English

实体框架:将实体与可空字段相关联的问题

[英]Entity Framework: Problem associating entities with nullable field

I'm using Entity Framework, and I'm trying to associate an entity that was created from a database table with an entity that was created from a database view. 我正在使用实体框架,我正在尝试将从数据库表创建的实体与从数据库视图创建的实体相关联。 Because Entity Framework is not able to infer the relationships between a database table and a view automatically, I'm using the entity designer to construct an "association" between the entities. 因为实体框架无法自动推断数据库表和视图之间的关系,所以我使用实体设计器来构造实体之间的“关联”。 However, if the foreign key is a nullalbe type, I get the following error: 但是,如果外键是nullalbe类型,我会收到以下错误:

Error 113: Multiplicity is not valid in Role 'Company' in relationship 'UsersView'. 错误113:多重性在关系“UsersView”中的角色“公司”中无效。 Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be '0..1'. 由于“从属角色”中的所有属性都可以为空,因此“主体角色”的多重性必须为“0..1”。

In my scenario, I have a CompanyId foreign key in my UsersView that's nullable (ie users may not have a company). 在我的场景中,我的UsersView中有一个可以为空的CompanyId外键(即用户可能没有公司)。 Creating an association with a nullable foreign key was never a problem with Linq 2 SQL. 使用可以为空的外键创建关联从来不是Linq 2 SQL的问题。 Does anyone know how I can get around this problem in Entity Framework? 有谁知道如何在实体框架中解决这个问题?

Thanks in advance. 提前致谢。

I think you should be able to select the Association, and set the Multiplicity setting in the Properties pane to 0..1. 我认为您应该能够选择关联,并将“属性”窗格中的“多重性”设置设置为0..1。 That should resolve the error. 这应该可以解决错误。

I'm posting this because I ran into the same error manually creating an association in the model designer in Visual Studio 2010. My field didn't allow nulls in the database but for some reason in the designer the Nullable property for that field was set to (None) instead of True. 我发布这个是因为我在Visual Studio 2010中手动创建模型设计器中的关联时遇到了同样的错误。我的字段不允许数据库中的空值但由于某些原因在设计器中设置了该字段的Nullable属性到(无)而不是真。 It took me a while to figure out why the designer was complaining. 我花了一段时间才弄清楚设计师为什么抱怨。 Once I manually changed the Nullable property of field I was trying to create an association to it without any errors. 一旦我手动更改了字段的Nullable属性,我就试图创建一个没有任何错误的关联。

I know this doesn't answer the original question but I found this post trying to solve my issue which was giving the same error. 我知道这不回答原来的问题,但我发现这篇文章试图解决我的问题,这也是同样的错误。

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

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