.WithMany() 和 .WithOptional() 之间的区别?

发布于 2024-10-26 09:49:30 字数 680 浏览 4 评论 0原文

下面是两个类似的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2024-11-02 09:49:30

如果您的模型如下所示:

public class Country
{
    public int CountryId { get; set; }
    public Currency Currency { get; set; }
}

public class Currency
{
    public int CurrencyId { get; set; }
}

那么……

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithOptional()
            .WillCascadeOnDelete(false);

在数据库中创建一个外键关系,其中 Countries 表中的 CountryId 是主键,外键是同时使用 Currency 表的 CurrencyId,因此 Countries只有一列 国家/地区IDCurrency 记录可以在没有相关 Countries 记录的情况下存在。但是,如果 Currency 记录具有相关的 Countries 记录,则不能多于一个,因为外键是 CountryId,它是同一记录的主键时间,因此只能出现在一条记录中。所以关系 Currency ->国家/地区1-to-0...1

另一个示例......

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithMany()
            .WillCascadeOnDelete(false);

在数据库的 Countries 表中创建第二列 CurrencyId,该表不可为 null,并且是Currency 表的 CurrencyId 的外键。因此,这里的 Currency 记录可能没有相关的 Countries 记录,或者有一个或多个相关记录,因为外键现在是另一列,与主键不同。因此,Countries 表中的多行可能具有相同的外键。关系货币->国家/地区在这里是1-to-0...n

编辑

如果您对两个不同配置的模型采用以下代码……

Country country1 = new Country();
Country country2 = new Country();
Currency currency = new Currency();

country1.Currency = currency;
country2.Currency = currency;

context.Countries.Add(country1);
context.Countries.Add(country2);

context.SaveChanges();

那么第二种情况 (.WithMany) 有效:我们在数据库中获得两个新的国家/地区和一种货币。

然而有点奇怪的是,在第二种情况(.HasOptional)中,仅存储第一个国家/地区,而忽略第二个国家/地区。事实上,我原以为会得到例外。我不确定这是否必须被视为一个错误。

Edit2

将上例中的顺序更改为 ...

context.Countries.Add(country1);
context.Countries.Add(country2);

country1.Currency = currency;
country2.Currency = currency;

... 会在“.HasOptional”情况下引发预期的异常。

If your model would look like this:

public class Country
{
    public int CountryId { get; set; }
    public Currency Currency { get; set; }
}

public class Currency
{
    public int CurrencyId { get; set; }
}

then ...

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithOptional()
            .WillCascadeOnDelete(false);

... creates a foreign key relationship in the database where CountryId in the Countries table is primary key and foreign key to the CurrencyId of the Currencies table at the same time, so the Countries table has only one single column CountryId. A Currencies record can live without a related Countries record. But if a Currencies record has a related Countries record then not more than one because the foreign key is CountryId which is the primary key at the same time and can therefore only be in one record. So the relationship Currencies -> Countries is 1-to-0...1.

The other example ...

modelBuilder.Entity<Country>()
            .HasRequired(cou => cou.Currency)
            .WithMany()
            .WillCascadeOnDelete(false);

... creates a second column CurrencyId in the Countries table of the database which is non-nullable and is a foreign key to the CurrencyId of the Currencies table. So here it is possible that a Currencies record has no related Countries 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 the Countries table may have the same foreign key. The relationship Currencies -> Countries here is 1-to-0...n.

Edit

If you take the following code for the two differently configured models ...

Country country1 = new Country();
Country country2 = new Country();
Currency currency = new Currency();

country1.Currency = currency;
country2.Currency = currency;

context.Countries.Add(country1);
context.Countries.Add(country2);

context.SaveChanges();

... 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 ...

context.Countries.Add(country1);
context.Countries.Add(country2);

country1.Currency = currency;
country2.Currency = currency;

... throws the expected exception in the ".HasOptional" case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文