entity framework向上迁移table关系不对?

entity framework migration up table relationships incorrect?

演示三种关系的简单模型:

一对一 - 地契和房子:房子有 1 个地契,1 个地契属于那个房子

一对多 - 房子 -> 房间:我的房子有很多房间

多对多 - 房间 <-> 颜色:许多房间可以有许多相同的颜色

类(没有构造函数):

 public class Deed   {
     public int DeedId { get; set; }
     public string Owner { get; set; }
     public House Home { get; set; }

  public class House
  {
    public int HouseId { get; set; }
    public string Address { get; set; }
    public List<Room> Rooms { get; set; }

  public class Room
  {
    public int RoomId { get; set; }
    public string Name { get; set; }
    List<Color> Colors { get; set; }

  public class Color
  {
    public int ColorId { get; set; }
    public string ColorName { get; set; }
    public List<Room> Rooms { get; set; }

我的困惑在于 Migration UP 构造函数: 首先是一对一:当 Deed 和 House 互相指出无法确定“原则”时,我得到一个错误。所以我把Deed作为原则:

下一个房子有很多房间。我想我明白外键只在每个房间里指向房子,像这样:

接下来是我真正感到困惑的地方。我希望 Many ROOMS 共享 Many COLORS(多对多)……每个房间都会指向另一个。但是:

感谢您的建议。 提前致谢, 瑜珈(“查克”)

尝试对您的模型进行这种小的清理。还有为什么一个房子不能有多个契约?无论如何,EF6 很难对 1-1 关系进行建模。您要么必须将 DeedId 设为房屋的 FK,要么将 Deed 设为复杂类型而不是实体。

public class Deed
{
    public int DeedId { get; set; }
    public string Owner { get; set; }
    public int HouseId { get; set; }
    public House House { get; set; }
}

public class House
{
    public int HouseId { get; set; }
    public string Address { get; set; }
    public virtual ICollection<Room> Rooms { get; } = new HashSet<Room>();
}
public class Room
{
    public int RoomId { get; set; }
    public string Name { get; set; }
    public int HouseId { get; set; }
    public House House { get; set; }
    public virtual ICollection<Color> Colors { get; } = new HashSet<Color>();
}
public class Color
{
    public int ColorId { get; set; }
    public string ColorName { get; set; }
    public virtual ICollection<Room> Rooms { get; } = new HashSet<Room>();

}
 public class MyDbContext : DbContext
{
    public DbSet<Deed> Deeds { get; set; }
    public DbSet<House> Houses { get; set; }
    public DbSet<Room> Rooms { get; set; }
    public DbSet<Color> Colors { get; set; }

}