开发者

Not sure how to fix this weird Entity Framework (relationship/navigation) error

I keep getting the following error message when i try to save a poco entity, using Entity Framework as the OR/M :-

Entities in 'SqlServerContext.Foos' participate in the 'FK_Foos_Bahs' relationship. 0 related 'Bah' were found. 1 'Bah' is expected.

Ok - the error message makes sense -- but that is NOT what I modeled :( (or am trying to model). It's saying that if I wish to save a Foo, then I need 1 instance of a Bah. A 开发者_开发技巧Foo can exist without a Bah. The relationship should be 1 <-> 0-or-1 .. not 1 <-> 1.

Here's the model in EF ...

Not sure how to fix this weird Entity Framework (relationship/navigation) error

Not sure how to fix this weird Entity Framework (relationship/navigation) error

Not sure how to fix this weird Entity Framework (relationship/navigation) error

Can anyone see what I've done wrong?


Your model is probably wrong. At the moment you are saying that Bah is principal entity of many Foos. It is one-to-many relation not one-to-one. Also you defined Bah as mandatory in Foo because multiplicity on Bah is 1 not 0..1.


Looks like your relationship is back to front - your screen grab shows that 1 Bah has many Foos and Foo has exactly 1 Bah - it sounds like you need to swap the ends round so that 1 Foo can have many Bahs and Bah has exactly 1 Foo.

You can also do 0...1 to many relationships (each Bah can have many Foos, Bahs have 0 or 1 Foo), or 0...1 to 1 relationships. .Net will make nullable fields in the database and nullable properties in your model, and you'll be allowed to save data without the associations.

I know you're probably working with sensitive data hence the blanking out, but maybe if you swapped it to a more meaningful fake it'd be easier to understand what you're trying to do! Foo and Bah make a less intuitive demonstration than something equally sanitised like People and Pets or Hobbies or whatever.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜