Composite Key and using Multiple Column Attributes in Entity Framework 6

c# entity-framework entity-framework-6

Question

When applying code to pre-existing fields in a table in a database, I'm attempting to construct a composite key utilizing two columns;

    [Key,Column("driverId", Order=0)]
    [JsonProperty(PropertyName="driverid")]
    public override int ID { get; set; }

    [Key,Column("type", Order=1)]
    [JsonProperty(PropertyName="typeid")]
    public int Type { get; set; }

I now encounter the following issue when attempting to execute a new migration:

The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical. DriversToVehicle_Driver_Target_DriversToVehicle_Driver_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

The following is the DriversToVehicle table:

public partial class DriversToVehicle
{
    [Column("id"), Key]
    public int ID { get; set; }

    [Column("driverid")]
    public int DriverID { get; set; }
    [ForeignKey("DriverID")]
    public Driver Driver { get; set; }

    [Column("vehicleid")]
    public int VehicleID { get; set; }
    [ForeignKey("VehicleID")]
    public Vehicle Vehicle { get; set; }
}

Adding to this query, the ID's original single key, i.e.

    [Column("driverId")]
    [JsonProperty(PropertyName="driverid")]
    public override int ID { get; set; }

How will this affect other things that link to it going forward (by this, I mean the code first in the classes)? Will ef resolve this on its own? Or do I now need to link to this class using both keys in other entities?

for instance, like before, I would have

public virtual Driver myDriver;

It is obvious that now, in addition to the ID, there also needs to be a link to the Type.

I appreciate it.

CHANGE FOR ANSWER

Okay, I separated the Type into its own class. How do I designate the foreign key as also being a composite key is currently the major problem.

The following are my classes:

    public partial class DriverType 
    {
        [Column("Id")]
        [JsonProperty(PropertyName = "drivertypeid")]
        public override int ID { get; set; }

        [JsonProperty(PropertyName = "drivertype")]
        public string Name { get; set; }
    }

Then (to keep things brief) I have the following in the Driver

public partial class Driver : AuditableEntity<int>
{
    [Key,Column("driverId", Order=0)]
    [JsonProperty(PropertyName="driverid")]
    public override int ID { get; set; }

    [Key,Column("type", Order=1)]
    [ForeignKey("DriverType")]
    [JsonProperty(PropertyName="drivertypeid")]
    public int DriverTypeId { get; set; }
    public DriverType DriverType { get; set; }

Please tell me how to add it to the DriverToVehicle class after that. I have so far

DriversToVehicle is a public partial class with the following members: [Column("id"), Key] public int ID, get, set.

    [Column("driverid", Order=0), ForeignKey("Driver")]
    public int DriverID { get; set; }
    public Driver Driver { get; set; }

    [Column("type", Order = 1), ForeignKey("Driver")] 
    public int DriverTypeId { get; set; }

    [ForeignKey("DriverTypeId")]
    public DriverType DriverType { get; set; }

}

Is it just me, or does this not seem right?

1
0
9/28/2015 6:00:56 PM

Popular Answer

as of yourDrivers primary key of the table is now(DriverId, Type) so that you can't refer to your drivers byDriverId you must refer to them by both, not just one.DriverId and Type Consequently, yourDriversToVehicle Table should have the following appearance:

public partial class DriversToVehicle
{
    [Column("id"), Key]
    public int ID { get; set; }

    [Column("driverid")]
    public int DriverID { get; set; }
    [ForeignKey("DriverID")]
    public Driver Driver { get; set; }

    [Column("DriverType")]
    public int DriverType { get; set; }
    [ForeignKey("type")]
    public int DriverType { get; set; }

    [Column("vehicleid")]
    public int VehicleID { get; set; }
    [ForeignKey("VehicleID")]
    public Vehicle Vehicle { get; set; }
}

However, as @hopeless notes above, if you accurately model your join table, you may not need to model this join table.Driver and Vehicle typing is accurate.

HTH.

0
9/28/2015 5:20:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow