gpt4 book ai didi

c# - 如何在 Entity Framework 6 的数据注释中添加复合外键

转载 作者:行者123 更新时间:2023-11-30 15:21:15 25 4
gpt4 key购买 nike

我正在使用 Entity Framework 6 开发 ASP.NET 应用程序。我已经有了数据库,并且正在使用 Code First 现有的数据库方法。

我有两个表,第二个表有外键和它自己的主键。我使用了 Column order 但仍然出现以下错误。我已经在线搜索,尝试了不同的组合,但仍然出现错误。

'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 '*'.

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; }
}

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

最佳答案

试试这个

[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; }
}

B 类中的外键链接到表而不是属性。

我相信上面的代码应该会给你正确的结果

关于c# - 如何在 Entity Framework 6 的数据注释中添加复合外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com