简体   繁体   English

使用实体框架,如何在两个模型上添加外键以相互引用

[英]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 . 在我的项目中,我有两个模型, RuleMenuItem 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.

相关问题 如何在Entity Framework的外键字段中添加数据? - How can I add data in foreign key field in Entity Framework? 如何使用实体框架保存外键? - How can I save a foreign key using Entity Framework? 如何使用实体框架查询外键对象? - How can I query the foreign key objects using Entity Framework? 如何在Entity Framework中创建由外键组成的复合主键? - How can I create a composite primary key consisting of foreign keys to two tables in Entity Framework? 实体框架5添加具有其他外键关系的实体和子实体 - Entity Framework 5 add entity and child entities with other foreign key relationships 如何在 Entity Framework Core 中指定一对一关系,其中双方都有一个外键? - How do I specify a one to one relationship in Entity Framework Core where both sides have a foreign key to each other? 如何使用实体框架更改外键关联? - How do I change a foreign key association using Entity Framework? 如何在Entity Framework中实现两个不同的外键? - How I can achieve two different foreign keys in Entity Framework? 如何使用Entity框架从多个其他模型创建一个模型的外来关系? - How to create a foreign relation to one Model from multiple other models using Entity framework? 我应该如何禁用每个对象的实体框架表引用(外部)列表? - How should I disable Entity Framework table reference(foreign) list from each objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM