具有每层次表的一对多实体框架为每个子类创建一个外键列

时间:2014-01-02 21:56:16

标签: c# entity-framework one-to-many table-per-hierarchy

我有Garage,其中包含CarsMotorcycles。汽车和摩托车是Vehicles。他们在这里:

public class Garage
{
    public int Id { get; set; }
    public virtual List<Car> Cars { get; set; }
    public virtual List<Motorcycle> Motorcycles { get; set; }

    public Garage()
    {
        Cars = new List<Car>();
        Motorcycles = new List<Motorcycle>();
    }
}

public abstract class Vehicle
{
    public int Id { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
}

public class Car : Vehicle
{
    public int GarageId { get; set; }
    public virtual Garage Garage { get; set; }
    // some more properties here...
}

public class Motorcycle : Vehicle
{
    public int GarageId { get; set; }
    public virtual Garage Garage { get; set; }
    // some more properties here...
}

为什么Car和Motorcycle都有GarageId和Garage属性?如果我将这些属性推送到Vehicle超类,EF会抱怨并告诉我导航属性必须位于具体的类中。

继续,这是我的DbContext:

public class DataContext : DbContext
{
    public DbSet<Garage> Garages { get; set; }
    public DbSet<Vehicle> Vehicles { get; set; }
    public DbSet<Car> Cars { get; set; }
    public DbSet<Motorcycle> Motorcycles { get; set; }

    public DataContext()
        : base("GarageExample")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
    }
}

这是一个与玩具玩的简短程序:

class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer<DataContext>(new DropCreateDatabaseAlways<DataContext>());

        using (var db = new DataContext())
        {
            var car1 = new Car { Make = "Subaru", Model = "Legacy" };
            var car2 = new Car { Make = "Porche", Model = "911" };

            var bike1 = new Motorcycle { Make = "Suzuki", Model = "GS500" };
            var bike2 = new Motorcycle { Make = "Kawasaki", Model = "Ninja" };

            var garage = new Garage();

            garage.Cars.Add(car1);
            garage.Cars.Add(car2);
            garage.Motorcycles.Add(bike1);
            garage.Motorcycles.Add(bike2);

            db.Garages.Add(garage);

            db.SaveChanges();
        }
    }
}

程序运行,并生成以下车辆表:

Id Make     Model  GarageId GarageId1 Discriminator
1  Subaru   Legacy 1        null      Car
2  Porche   911    1        null      Car
3  Suzuki   GS500  null     1         Motorcycle
4  Kawasaki Ninja  null     1         Motorcycle

Car和Motorcycle都拥有自己的GarageId和Garage属性,似乎每个子类都在创建自己的车库外键。如何告诉EF(通过流畅的api,如果可能的话)Car.Garage和Motorcycle.Garage是一样的,应该使用相同的列?

这是我想要的车辆表格,当然:

Id Make     Model  GarageId Discriminator
1  Subaru   Legacy 1        Car
2  Porche   911    1        Car
3  Suzuki   GS500  1        Motorcycle
4  Kawasaki Ninja  1        Motorcycle

4 个答案:

答案 0 :(得分:4)

在汽车和摩托车类的GarageId属性中使用属性[Column(&#34; GarageId&#34;)]。

答案 1 :(得分:3)

我知道获得单个外键列和所需数据库模式的唯一方法是放弃Garage中每个派生类型的导航集合,而是使用单个集合作为基类型:

public class Garage
{
    public int Id { get; set; }
    public virtual List<Vehicle> Vehicles { get; set; }

    public Garage()
    {
        Vehicles = new List<Vehicle>();
    }
}

public abstract class Vehicle
{
    public int Id { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }

    public int GarageId { get; set; }
    public virtual Garage Garage { get; set; }
}

public class Car : Vehicle
{
    // some more properties here...
}

public class Motorcycle : Vehicle
{
    // some more properties here...
}

当你只想加载Car的{​​{1}}或Motorcycle s并且你必须加载所有内容时,你正在丢失带有懒惰或急切加载的舒适类型过滤器Garage的{​​{1}}或使用投影或显式加载来加载派生类型。

在我看来,它是完全有效的你正在尝试做什么,但实际上它不支持实体框架,或者没有以支持这种情况的方式实现到FK列的映射。

答案 2 :(得分:0)

    public class Garage
    {
        public int Id { get; set; }
        public virtual List<Car> Cars { get; set; }
        public virtual List<Motorcycle> Motorcycles { get; set; }

        public Garage()
        {
            Cars = new List<Car>();
            Motorcycles = new List<Motorcycle>();
        }
    }

    public abstract class Vehicle
    {
        public int Id { get; set; }
        public int GarageId { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
    }

    public class Car : Vehicle
    {
        [ForeignKey("GarageId")]
        public virtual Garage Garage { get; set; }
        // some more properties here...
    }

    public class Motorcycle : Vehicle
    {
        [ForeignKey("GarageId")]
        public virtual Garage Garage { get; set; }
        // some more properties here...
    }

答案 3 :(得分:-1)

你看过这个了吗?

  

映射每层次表(TPH)继承

     

在TPH映射方案中,继承层次结构中的所有类型都是   映射到单个表。鉴别器列用于识别   每行的类型。使用Code First创建模型时,TPH就是   参与继承的类型的默认策略   层次结构。默认情况下,将鉴别器列添加到表中   名称为“Discriminator”,其中包含每种类型的CLR类型名称   层次结构用于鉴别器值。你可以修改   使用Fluent API的默认行为。

modelBuilder.Entity<Course>() 
.Map<Course>(m => m.Requires("Type").HasValue("Course")) 
.Map<OnsiteCourse>(m => m.Requires("Type").HasValue("OnsiteCourse"));

直接来自here.