简体   繁体   中英

How using entity framework fluent API to config such relationship?

1.A car may have one dealer if it is delivered out of the storage(the car belong to the dealer),otherwise,A car has no dealer.

2.A dealer may have a collection of cars or not. The code is below

public Car
{
  public long Id {get;set;}
  public Dealer Dealer{get;set;}
}

public Dealer
{
  public long Id{get;set;}
  public ICollection<Car> Cars {get;set;}
}

You can try this:

modelBuilder.Entity<Car>()
            .HasOptional(m => m.Dealer)
            .WithMany(m => m.Cars);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM