.WithMany() 和 .WithOptional() 之间的区别?
下面是两个类似的 Fluent API 配置:
WithMany()
modelBuilder.Entity<Country>()
.HasRequired(cou => cou.Currency)
.WithMany()
.WillCascadeOnDelete(false);
WithOptional()
modelBuilder.Entity<Country>()
.HasRequired(cou => cou.Currency)
.WithOptional()
.WillCascadeOnDelete(false);
我在这里想表达的是:每个 Country
都需要一个具体的 < code>Currency,但 Currency
可以分配给零个、一个或多个国家。
我必须使用上述哪一个语句?或者换句话说: .WithMany()
和 .WithOptional()
运算符之间到底有什么区别?
Below are two similar fluent API configurations:
WithMany()
modelBuilder.Entity<Country>()
.HasRequired(cou => cou.Currency)
.WithMany()
.WillCascadeOnDelete(false);
WithOptional()
modelBuilder.Entity<Country>()
.HasRequired(cou => cou.Currency)
.WithOptional()
.WillCascadeOnDelete(false);
What I am trying to express here is: Every Country
requires a concrete Currency
, but a Currency
can be assigned to zero, one or many Countries.
Which of the above statements would I have to use? Or in other words: What exactly is the difference between .WithMany()
and .WithOptional()
operators?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的模型如下所示:
那么……
在数据库中创建一个外键关系,其中
Countries
表中的CountryId
是主键,外键是同时使用Currency
表的CurrencyId
,因此Countries
表只有一列国家/地区ID
。Currency
记录可以在没有相关Countries
记录的情况下存在。但是,如果Currency
记录具有相关的Countries
记录,则不能多于一个,因为外键是CountryId
,它是同一记录的主键时间,因此只能出现在一条记录中。所以关系Currency ->国家/地区
为1-to-0...1
。另一个示例......
在数据库的
Countries
表中创建第二列CurrencyId
,该表不可为 null,并且是Currency
表的CurrencyId
的外键。因此,这里的Currency
记录可能没有相关的Countries
记录,或者有一个或多个相关记录,因为外键现在是另一列,与主键不同。因此,Countries
表中的多行可能具有相同的外键。关系货币->国家/地区
在这里是1-to-0...n
。编辑
如果您对两个不同配置的模型采用以下代码……
那么第二种情况 (.WithMany) 有效:我们在数据库中获得两个新的国家/地区和一种货币。
然而有点奇怪的是,在第二种情况(.HasOptional)中,仅存储第一个国家/地区,而忽略第二个国家/地区。事实上,我原以为会得到例外。我不确定这是否必须被视为一个错误。
Edit2
将上例中的顺序更改为 ...
... 会在“.HasOptional”情况下引发预期的异常。
If your model would look like this:
then ...
... creates a foreign key relationship in the database where
CountryId
in theCountries
table is primary key and foreign key to theCurrencyId
of theCurrencies
table at the same time, so theCountries
table has only one single columnCountryId
. ACurrencies
record can live without a relatedCountries
record. But if aCurrencies
record has a relatedCountries
record then not more than one because the foreign key isCountryId
which is the primary key at the same time and can therefore only be in one record. So the relationshipCurrencies -> Countries
is1-to-0...1
.The other example ...
... creates a second column
CurrencyId
in theCountries
table of the database which is non-nullable and is a foreign key to theCurrencyId
of theCurrencies
table. So here it is possible that aCurrencies
record has no relatedCountries
record or one or more than one because the foreign key is now another column, not identical with the primary key. Therefore more than one row in theCountries
table may have the same foreign key. The relationshipCurrencies -> Countries
here is1-to-0...n
.Edit
If you take the following code for the two differently configured models ...
... then the second case (.WithMany) works: We get two new Countries and one Currency in the database.
However a bit strange is that in the second case (.HasOptional) only the first Country is stored, the second is simply ignored. Actually I had expected to get an exception. I am not sure if that has to be considered as a bug.
Edit2
Changing the order in the example above to ...
... throws the expected exception in the ".HasOptional" case.