不知道如何解决这个奇怪的实体框架(关系/导航)错误 [英] Not sure how to fix this weird Entity Framework (relationship/navigation) error
问题描述
我不断收到以下错误消息,当我尝试保存POCO实体,使用实体框架作为OR / M: -
在'SqlServerContext.Foos实体参加FK_Foos_Bahs的关系。有关'呸'0被发现了。 1'呸'的预期。
块引用>
块引用>确定 - 错误信息是有道理的 - 但是,这并不是什么我模仿:((或正在尝试模拟)这是说,如果我想保存
富
,那么我需要1实例呸
A富
没有一个能够存在。呸
的关系应该是1< - > 0或1
..不是1< - 方式> 1
下面是EF模型......
任何人都可以看到我做了什么错?
解决方案看起来像你们的关系是从后到前 - 你的屏幕抓取显示,1呸有许多FOOS与富恰好有1呸 - 这听起来像你需要交换两端圆形,这样1美孚可以有很多Bahs和Bah恰好有1美孚。
您也可以做到0 ... 1对多的关系(每个呸可以有很多FOOS,Bahs有0或1的Foo),或0 ... 1比1的关系。 .NET将在数据库中可空字段和可为空的属性在模型中,你会被允许来保存数据不关联。
我知道你可能会处理敏感数据因此而消隐了,但也许如果你把它换到一个更有意义的假它会更容易理解你想要做什么!美孚和Bah做出不太直观演示比同样的东西像消毒,人与宠物或爱好或什么的。
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 aBah
. AFoo
can exist without aBah
. The relationship should be1 <-> 0-or-1
.. not1 <-> 1
.Here's the model in EF ...
Can anyone see what I've done wrong?
解决方案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.
这篇关于不知道如何解决这个奇怪的实体框架(关系/导航)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!