0

这个想法是一个房间里有家具。很简单的事情。这是弗尼模型:

public class Furni
{
    public int ID { get; set; }
    public string Name { get; set; }
}
public class FurniDbContext : DbContext
{
    public DbSet<Furni> Furniture { get; set; }
}

当然,它工作正常。它与应有的表格耦合。现在我正在编写房间模型:

public class Room
{
    public int ID { get; set; }
    public Furni Furni { get; set; } <<<<< I have no idea how to couple it to FurniDbContext
}
public class RoomDbContext : DbContext
{
    public DbSet<Room> Rooms { get; set; }
}

有什么帮助吗?:) 我希望我已经足够清楚了。

4

1 回答 1

0

您不需要DbContext为每个班级单独设置。它看起来像这样:

房间

public class Room
{
    public int ID { get; set; }
    public virtual Furni Furniture { get; set; } // virtual is for lazy loading
}

家具

public class Furni
{
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual List<Room> Rooms { get; set; }  // if you want to have the relationship both ways
}

数据库上下文

public class AppDbContext : DbContext
{
    public DbSet<Furni> Furniture { get; set; }
    public DbSet<Room> Rooms { get; set; }
}

希望有帮助。

于 2013-09-05T12:43:31.857 回答