开发者

Entity Framework - Nullable Foreign Key error

开发者 https://www.devze.com 2023-01-03 15:42 出处:网络
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

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 cha开发者_如何学编程nging 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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消