EF 4.1:.WithMany()和.WithOptional()之间的区别? [英] EF 4.1: Difference between .WithMany() and .WithOptional() ?

查看:705
本文介绍了EF 4.1:.WithMany()和.WithOptional()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是两种类似流畅的API配置:

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 have zero, one or many Countries assigned.

我将要使用以上哪些语句?或者换句话说: .WithMany() .WithOptional()运算符之间的区别究竟是什么? p>

Which of the above statements would I have to use? Or in other words: What exactly is the difference between .WithMany() and .WithOptional() operators?

推荐答案

如果你的模型看起来像这样:

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; }
}

然后...

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

...在数据库中创建一个外键关系,其中 CountryId < 国家表中的code>是主要键, CurrencyId 的外键是货币表同时,所以国家表只有一个单列 CountryId 。一个货币记录可以没有相关的国家记录。但是如果货币记录具有相关的国家记录,则不超过一个,因为外键为 CountryId 它是同一时间的主键,因此只能在一个记录中。所以关系货币 - >国家 1到0 ... 1

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

另一个例子...

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

...创建第二列 CurrencyId 在不可为空的数据库的 Countries 表中,是 CurrencyId c $ c>货币表。所以这里有一个货币与国家之间没有相关的记录,或者一个或多个,因为外键现在是另一个列,与主键不同。因此, Countries 表中的多行可能具有相同的外键。关系货币 - >国家这里是 1到0 ... n

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

编辑

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

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();

...然后第二种情况(.WithMany)工作:我们得到两个新的国家和一个货币在数据库中。

... then the second case (.WithMany) works: We get two new Countries and one Currency in the database.

然而有一点奇怪的是,在第二种情况(.HasOptional)中只有第一个国家被存储,第二个被简单地忽略。其实我曾经有过例外。我不知道这是否被认为是一个错误。

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;

...在.HasOptional案例中抛出预期的异常。

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

这篇关于EF 4.1:.WithMany()和.WithOptional()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆