Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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.

like image 912
Johnny Oshika Avatar asked Nov 28 '10 23:11

Johnny Oshika


2 Answers

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.

like image 79
Dave Swersky Avatar answered Nov 09 '22 11:11

Dave Swersky


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.

like image 35
Adam Avatar answered Nov 09 '22 10:11

Adam