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

I am working on an ASP.NET application with Entity Framework 6. I already have a database, and I'm using the Code First existing Database approach.

I have two tables, and the second table has a Foriegn key along with its own primary key. I used the column order, but still getting the following error. I have an internet search, try a different combination, but still get an 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

+4
source share
3 answers

try it

[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 that you have in class B is bound to a table, not a property.

,

+2

, , , , , .

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

    }

    [Key, ForeignKey("Sys_Nav_FunctionEntity")]
    [Required]
    public int Function_ID { get; set; }

    public int Parent_Function_ID { get; set; }

    public Sys_Nav_FunctionEntity Sys_Nav_FunctionEntity { get; set; }
}
+2

I think the ForeignKey attribute should be set in the navigation property:

[Table("Sys_Nav_FunctionHierarchy")]
public class Sys_Nav_FunctionHierarchyEntity
{
    //...
    public int Function_ID { get; set; }

    [ForeignKey("Function_ID")]
    public Sys_Nav_FunctionEntity Sys_Nav_FunctionEntity { get; set; }
}

If Parent_Function_ID is also a foreign key to the same table, you can do it the same way.

Also in the first grade:

public Sys_Nav_FunctionHierarchyEntity Sys_Nav_FunctionHierarchyEntity { get; set; }

should be an assembly (if I understood probem correctly)

+1
source

All Articles