希望有人能告诉我如何实现这种多对可能的关系......
我有两个班级,汽车和车主。每辆车可以由多个车主拥有。每个车主可以拥有多辆汽车。请注意,我只想要从汽车到车主的导航属性。
public partial class Car
{
public int CarId { get; set; }
public string Name { get; set; }
public ICollection<Owner> Owners { get; set; }
}
public partial class Owner
{
public int OwnerId { get; set; }
public string Name { get; set; }
}
在我的 DbInitializer 中,我像这样播种 b 。
Owner owner1 = new Owner() { OwnerId = 10, Name = "Erik" };
context.Owners.Add(owner1);
Car b1 = new Car() { CarId = 1, Name = "Volvo", Owners = new List<Owner>(new Owner[] { owner1 }) };
Car b2 = new Car() { CarId = 2, Name = "Saab", Owners = new List<Owner>(new Owner[] { owner1 }) };
context.Cars.Add(b1);
context.Cars.Add(b2);
context.SaveChanges();
结果是 owner1 只拥有 b2。为什么?以及如何将其更改为多对多关系?
谢谢!