[英]Using Entity Framework, how can I add a foreign key on two models to reference each other
I have a ASP.NET MVC 5/C# project. 我有一个ASP.NET MVC 5 / C#项目。 In my project I have two models,
Rule
and MenuItem
. 在我的项目中,我有两个模型,
Rule
和MenuItem
。 MenuItem
has a foreign key that references Rule
. MenuItem
有一个引用Rule
的外键。 And Rule
has a foreign key that references MenuItem
. 而
Rule
有一个引用MenuItem
的外键。
Couple things worth mentioning, my model have a Prefix in the model name. 值得一提的是,我的模型在模型名称中有一个前缀。 Also, I am using database first approach.
另外,我使用数据库第一种方法。
I want to be able to get the MenuItem with the required rule using .Include(...)
and also I want to be able to get the Rules with the MenuItem 我希望能够使用
.Include(...)
获取具有所需规则的MenuItem,并且我希望能够使用MenuItem获取规则
Here are my models 这是我的模特
[Table("Rules")]
public class PrefixRule
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[ForeignKey("Item")]
public int ModuleId { get; set; }
public string Name { get; set; }
public virtual PrefixMenuItem Item { get; set; }
}
[Table("MenuItems")]
public class PrefixMenuItem
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
[ForeignKey("RequiredRule")]
public int? RequiredRuleId { get; set; }
public string Name { get; set; }
public virtual PrefixRule RequiredRule { get; set; }
}
However, when I try to pull the menu-items including the required-rule, I get the following error 但是,当我尝试拉出包含必需规则的菜单项时,我收到以下错误
One or more validation errors were detected during model generation:MenuItem_RequiredRule_Target: : Multiplicity is not valid in Role 'MenuItem_RequiredRule_Target' in relationship 'MenuItem_RequiredRule'.
在模型生成期间检测到一个或多个验证错误:MenuItem_RequiredRule_Target :: Multiplicity在关系'MenuItem_RequiredRule'中的角色'MenuItem_RequiredRule_Target'中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
由于“从属角色”属性不是关键属性,因此从属角色的多重性的上限必须为“*”。
I believe this error due to the circular references between my models. 我相信这个错误是由于我的模型之间的循环引用。 However, I need to be able to access both properties either way.
但是,我需要能够以任何方式访问这两个属性。
How can I fix this problem? 我该如何解决这个问题?
Entity Framework should be smart enough to figure out that this is 1 to 0/1 relationship. 实体框架应该足够聪明,以确定这是1到0/1的关系。 Not tested but this should work?
没有测试,但这应该工作?
public partial class Rule
{
[Key, ForeignKey("Item")]
public string ModuleId { get; set; }
public string Name { get; set; }
public virtual MenuItem Item { get; set; }
}
public partial class MenuItem
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
public string Name { get; set; }
public virtual Rule RequiredRule { get; set; }
}
If you are going this route then you have to make public string Id in the both the Primary and Foreign Key in your Rules table by decorating it with [Key, ForeignKey("PrefixMenuItem")] 如果您要使用此路由,则必须通过使用[Key,ForeignKey(“PrefixMenuItem”)]进行装饰,在规则表中的主键和外键中创建公共字符串Id
See this article for a full example: http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx 有关完整示例,请参阅此文章: http : //www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx
Updated Example: 更新示例:
public partial class Rule
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, ForeignKey("Item")]
public string Id { get; set; }
[ForeignKey("Module")]
public int ModuleId { get; set; }
public string Name { get; set; }
public virtual MenuItem Item { get; set; }
public virtual Module Module { get; set; }
}
public partial class MenuItem
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }
public string Name { get; set; }
public virtual Rule RequiredRule { get; set; }
}
public partial class Module
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ModuleId { get; set; }
public string Name { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.