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:

Error 113: Multiplicity is not valid in Role 'Company' in relationship 'UsersView'. Because all the properties in the Dependent Role are nullable, multiplicity of the Principal Role must be '0..1'.

In my scenario, I have a CompanyId foreign key in my UsersView that's nullable (ie users may not have a company). Creating an association with a nullable foreign key was never a problem with 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. 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. 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.

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.

链接地址: http://www.djcxy.com/p/37472.html

上一篇: 首先将外键关系添加到实体框架数据库

下一篇: 实体框架:关联具有可空字段的实体的问题