EF Foreign Key przy użyciu Fluent API
Oto moje modele. Mam mapowanie jeden do jednego dla pojazdu i kierowcy. Najpierw stworzę pojazd, a następnie zmapuję kierowcę do pojazdu.
public class Driver
{
public int Id { get; set; }
public String Name { get; set; }
public int VehicleId { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class Vehicle
{
public int Id { get; set; }
public String Name { get; set; }
public virtual Driver Driver { get; set; }
public int VehicleGroupId { get; set; }
public virtual VehicleGroup Vehicles { get; set; }
}
Chcę użyć właściwości VehicleId w klasie Driver, aby zachować identyfikator pojazdu, którym kierowca prowadzi.
Napisałem następujący kod Fluent API:
modelBuilder.Entity<Vehicle>()
.HasRequired(d => d.Driver)
.WithRequiredPrincipal();
Ale tworzy nową kolumnę w tabeli Sterowniki - Vehicle_VehicleId i mapuje ją do tabeli VehicleId on Vehicle. Chcę, aby tabela VehicleId of Driver została odwzorowana.
Jestem także zupełnie nowy w EF i Fluent API. Uważam to za bardzo mylące, wybierając między WithRequiredDependent i WithRequiredPrincipal. Byłoby miło, gdybyś mógł to opisać prostymi słowami. Dzięki.