What is the difference between.WithMany() and.WithOptional()?

ef-code-first entity-framework fluent-interface

Question

The following two fluent API settings are comparable:

WithMany()

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

WithOptional()

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

I'm attempting to convey the following:Country demands a certainCurrency , but aCurrency may be attributed to 0 or 1 or many different countries.

Which of the sentences listed above would I need to use? Or, to put it another way: Exactly what distinguishes.WithMany() and .WithOptional() operators?

1
24
8/30/2018 11:54:35 AM

Accepted Answer

If your model has this appearance:

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

... builds a connection between foreign keys in the database whereCountryId in theCountries table is a foreign key and primary key to theCurrencyId of theCurrencies table simultaneously, therefore theCountries there is just one column. on the tableCountryId . ACurrencies a record may exist without a connected objectCountries record. Yet if aCurrencies a linked recordCountries no more than one record, since the foreign key isCountryId which, since it is the main key simultaneously, can only be found in one record. hence, the connectionCurrencies -> Countries is 1-to-0...1 .

The second illustration...

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

produces another columnCurrencyId in theCountries non-nullable table in the database that serves as a foreign key to theCurrencyId of theCurrencies table. It is thus feasible that aCurrencies the record is unrelatedCountries Because the foreign key is now a different column and is not the same as the main key, there may be one record or several records. there are several rows in theCountries table might share a foreign key. The connectionCurrencies -> Countries this is1-to-0...n .

Edit

If you use the two models with different configurations in the following code...

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

When the second case (.WithMany) succeeds, we add one new Currency and two new Countries to the database.

Strangely, only the first Country is saved in the second case (.HasOptional), while the second is obliviously disregarded. Actually, I had anticipated a special circumstance. I'm not certain whether it qualifies as a bug.

Edit2

Changing the sequence in the previous example to...

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

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

... in the case of ".HasOptional" throws the anticipated exception.

36
3/24/2011 6:28:15 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow