Nullable Foreign Key Error in Entity Framework

entity-framework nullable

Question

I have a Website table that has a foreign key to a Client table. A Website isn't always going to have an associated Client but when I save a Website without specifying a client then it errors out, as Website.ClientID is an int32 (not nullable) and is set to 0.

I tried changing the relationship in my EF diagram to be an * next to Website and a 0..1 next to Client but then it gives an error saying that it can't do 0..1 for ClientID because it's not nullable. I then changed Website.ClientID to be nullable, however now it gives this error:

TothSolutions.msl(69,10) : error 3031: Problem in mapping fragments starting 
at line 69:Non-nullable column Website.ClientID in table Website is mapped to 
a nullable entity property.

How do I set up a foreign key relationship where the dependent object is nullable?

1
0
4/24/2013 1:52:52 PM

Accepted Answer

I forgot to take the not null constraint off the column in the database.

0
4/24/2013 1:54:26 PM

Popular Answer

I got this problem as well but fixed it by removing the table from my model and adding it again.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow