Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add composite foreign key in data annotation for Entity Framework 6

I am working on ASP.NET application with Entity Framework 6. I have already database and I am using Code First existing Database approach.

I am two table and second table has Foriegn key along with its own primary key. I have used Column order but still getting following error. I have search online, tried different combination but still getting error.

'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code Sys_Nav_FunctionHierarchyEntity_Sys_Nav_FunctionEntity_Source: : Multiplicity is not valid in Role  'Sys_Nav_FunctionHierarchyEntity_Sys_Nav_FunctionEntity_Source' in relationship 'Sys_Nav_FunctionHierarchyEntity_Sys_Nav_FunctionEntity'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

Class A

 [Table("Sys_Nav_Function")]
 public class Sys_Nav_FunctionEntity
 {
    public Sys_Nav_FunctionEntity()
    { }


    [Key]
    public int Function_ID { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Hierarchy Level")]
    [Display(Name = "Hierarchy Level")]
    public int Hierarchy_Level { get; set; }


    public Sys_Nav_FunctionHierarchyEntity Sys_Nav_FunctionHierarchyEntity { get; set; }

    public ICollection<Sys_Nav_FunctionInActionEntity> Sys_Nav_FunctionInActionEntity { get; set; }

    public ICollection<Sys_Nav_FunctionInControllerEntity> Sys_Nav_FunctionInControllerEntity { get; set; }
}

Class B

 [Table("Sys_Nav_FunctionHierarchy")]
public class Sys_Nav_FunctionHierarchyEntity
{
    public Sys_Nav_FunctionHierarchyEntity()
    {

    }


    [Key, Column(Order =0)]
    public int Hierarchy_ID { get; set; }

    [ForeignKey("Sys_Nav_FunctionEntity"), Column(Order =1)]
    public int Function_ID { get; set; }

    public int Parent_Function_ID { get; set; }

    public Sys_Nav_FunctionEntity Sys_Nav_FunctionEntity { get; set; }
}

enter image description here

like image 409
K.Z Avatar asked Jan 20 '26 10:01

K.Z


1 Answers

Try this

[Table("Sys_Nav_FunctionHierarchy")]
public class Sys_Nav_FunctionHierarchyEntity
{
    public Sys_Nav_FunctionHierarchyEntity()
    {

    }


    [Key, Column(Order =0)]
    public int Hierarchy_ID { get; set; }

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

    public int Parent_Function_ID { get; set; }

    public Sys_Nav_FunctionEntity Sys_Nav_FunctionEntity { get; set; }
}

The foriegn key you have in class B is linked to the table and not to the property.

I believe the above code should give you the correct result

like image 59
Ben Jones Avatar answered Jan 22 '26 23:01

Ben Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!