I'm using EF6 Code First and here's a simple model which reproduces my issue:
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. 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...
I've tried to use TPT strategy - the same problem, but mixing is between association and inheritance keys.
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.
I can't create a base class for a Car
and a Person
because in real app they are already participate in another hierarchies.
Am I using Entity Framework wrong or it really can't map one-to-one relationship along with inheritance to a database?
I think you can resolve your problem with this model:
This way, a
Person
and aCar
are associated with aGeoInfo
, and and when you want to find a person by their coordinates, you could do this:But as you can see, with this model you are going to have one-to many relationships between
Person
andGeoInfo
andCar
andGeoInfo
. I think this model could be more real because, eg, two people could have the same coordinates.