Is it possible to create a mapping which would fill my navigation property automatically, based on table's PK? That means I don't want to have a FK on any table. I know it is possible when there is Optional -> Required mapping. AFAIK no constraint is created on SQL Server, the relationship is "simulated" by EF6 in runtime.
|====================| |====================|
| Car | | Driver |
|====================| |====================|
| Id | 0..1 ----- 0..1 | Id |
|--------------------| |--------------------|
So that code _dbContext.Cars.Include(x => x.Driver).First(x => x.Id == 1); would return a car with driver if exists, or null driver if doesn't exist.
class Car
{
public int Id { get; set; }
public virtual Driver Driver { get; set; }
}
class Driver
{
public int Id { get; set; }
public virtual Car Car { get; set; }
}
The following mappings wants to create a DriverId in my Car:
class CarMapping : EntityTypeConfiguration<Car>
{
public CarMapping()
{
HasKey(x => x.Id);
}
}
class DriverMapping : EntityTypeConfiguration<Driver>
{
public CarMapping()
{
HasKey(x => x.Id);
HasOptional(x => x.Car)
.WithOptionalPrincipal(x => x.Driver);
}
}
EDIT
I checked with mentioned earlier required mapping:
class CarMapping : EntityTypeConfiguration<Car>
{
public CarMapping()
{
HasKey(x => x.Id);
}
}
class DriverMapping : EntityTypeConfiguration<Driver>
{
public CarMapping()
{
HasKey(x => x.Id);
HasOptional(x => x.Car)
.WithRequired(x => x.Driver);
}
}
And it works. EF6 does not perform any validation on Inserts nor Deletes... Is this a bug? Well at least it works for my case.