简体   繁体   English

实体框架6:具有继承关系的一对一关系

[英]Entity Framework 6: one-to-one relationship with inheritance

I'm using EF6 Code First and here's a simple model which reproduces my issue: 我正在使用EF6 Code First,这是一个重现我问题的简单模型:

abstract class GeoInfo
{
    public int Id { get; set; }
    public double CoordX { get; set; }
    public double CoordY { get; set; }
}

class PersonGeoInfo : GeoInfo
{
    [Required]
    public Person Person { get; set; }
}

class CarGeoInfo : GeoInfo
{
    [Required]
    public Car Car { get; set; }
}

class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual PersonGeoInfo PersonGeoInfo { get; set; }
}

class Car
{
    public int Id { get; set; }
    public string Number { get; set; }
    public virtual CarGeoInfo CarGeoInfo { get; set; }
}

And a context: 和上下文:

class MyContext : DbContext
{
    public DbSet<GeoInfo> GeoInfos { get; set; }
    public DbSet<PersonGeoInfo> PersonGeoInfos { get; set; }
    public DbSet<CarGeoInfo> CarGeoInfos { get; set; }
    public DbSet<Person> Persons { get; set; }
    public DbSet<Car> Cars { get; set; }
}

Entity Framework generates this database: 实体框架生成此数据库:

数据库模式

Look at GeoInfoes foreign keys constraints. 查看GeoInfoes外键约束。 They both are in one column and using this database is imposible. 它们都在同一列中,因此不可能使用该数据库。 But EF didn't warned me, it just throws database exception: The INSERT statement conflicted with the FOREIGN KEY... 但是EF没有警告我,它只是引发数据库异常: The INSERT statement conflicted with the FOREIGN KEY...

I've tried to use TPT strategy - the same problem, but mixing is between association and inheritance keys. 我尝试使用TPT策略-同样的问题,但是混合是在关联键和继承键之间。

I've tried to explicitly define foreign keys in model - didn't help. 我试图在模型中显式定义外键-没有帮助。 Nothing stops EF from generating a FK constraint in the same PK column. 没有什么可以阻止EF在同一PK列中生成FK约束。

I can't create a base class for a Car and a Person because in real app they are already participate in another hierarchies. 我无法为CarPerson创建基类,因为在实际应用中,它们已经参与了另一个层次结构。

Am I using Entity Framework wrong or it really can't map one-to-one relationship along with inheritance to a database? 我是否使用Entity Framework错误,或者它真的不能将一对一关系以及继承关系映射到数据库?

I think you can resolve your problem with this model: 我认为您可以使用此模型解决问题:

public class GeoInfo
{
    public int Id { get; set; }
    public double CoordX { get; set; }
    public double CoordY { get; set; }

}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }

    [ForeignKey("PersonGeoInfo")]
    public int? PersonGeoInfoId { get; set; }

    public virtual GeoInfo PersonGeoInfo { get; set; }
}

public class Car
{
    public int Id { get; set; }
    public string Number { get; set; }

    [ForeignKey("CarGeoInfo")]
    public int? CarGeoInfoId { get; set; }

    public virtual GeoInfo CarGeoInfo { get; set; }
}

This way, a Person and a Car are associated with a GeoInfo , and and when you want to find a person by their coordinates, you could do this: 这样一来,一个PersonCar都与相关的GeoInfo ,而当你想找到一个人通过自己的坐标,你可以这样做:

 int geoInfoId = 3;
 var person=_db.Persons.FirstOrDefault(p=>p.PersonGeoInfoId==geoInfoId);

But as you can see, with this model you are going to have one-to many relationships between Person and GeoInfo and Car and GeoInfo . 但是正如您所看到的,使用此模型,您将在PersonGeoInfoCarGeoInfo之间建立一对多关系。 I think this model could be more real because, eg, two people could have the same coordinates. 我认为该模型可能更真实,因为例如两个人可能具有相同的坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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