'Use the Fluent API to create a one-to-one relationship between two tables' alternate keys
I'm attempting to join two tables in two databases, where the tables have have auto-increment, primary keys and string, alternate keys. The alternate keys in each system's table have been populated to allow data to be compared (joined).
I have two classes:
Unit (system A)
public class Unit
{
[Key]
public int ID { get; set; }
// alternate key
public string UnitNumber { get; set; }
...
public Vehicle Vehicle { get; set; }
}
Vehicle (system B)
public class Vehicle
{
[Key]
public int VehicleSerialNo { get; set; }
// alternate key
public string VehicleID { get; set; }
...
public Unit Unit { get; set; }
}
I would like to create a one-to-one relationship between Unit.UnitNumber = Vehicle.VehicleID.
I tried to define this relationship, but have been unsuccessful:
modelBuilder.Entity<Vehicle>()
.HasAlternateKey("VehicleID");
modelBuilder.Entity<Unit>()
.HasAlternateKey("UnitNumber");
modelBuilder.Entity<Vehicle>()
.HasOne<Unit>()
.WithOne()
.HasForeignKey("VehicleID");
modelBuilder.Entity<Unit>()
.HasOne<Vehicle>()
.WithOne()
.HasForeignKey("UnitNumber");
What am I missing?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
