Mapping composite foreign key to composite primary key where the foreign key is also a primary key

data-annotations entity-framework entity-framework-6

Question

I want to create a composite key for disc type using the VM hostname, datetime, and name values. Foreign keys, or VM hostname and datetime of VirtualMachine class, should be referenced simultaneously with VM hostname and datetime of disc type class.

I did this, however it allows for the following exception: The datetime attribute of type "WebJob1.Historical.Disk" does not have a valid ForeignKeyAttribute. The dependent type 'WebJob1.Historical.Disk' did not have the navigation property 'Datetime'. The name value for the navigation property must be correct.

Anyone know the answer? Please also be aware that I am employing data annotation.

public class VirtualMachine
{

    [Key]
    [Column(Order = 0)]
    public string VM_Hostname { get; set; }
    [Key]
    [Column(Order = 1)]
    public DateTime Datetime;
    public virtual List<Disk> disks { get; set; }
}

 public class Disk
{
    [Key,ForeignKey("VirtualMachine"),Column(Order = 0)]
    public string VM_hostname { get; set; }
    [Key,ForeignKey("Datetime"), Column(Order = 1)]
    public DateTime datetime { get; set; }
    [Key, Column(Order = 2)]
    public string name { get; set; }

    public virtual VirtualMachine VirtualMachine{ get; set; }


}
1
6
3/15/2017 7:48:13 PM

Accepted Answer

ZZZ_tmp
6
1/19/2019 8:07:29 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