ASP.NET MVC 4多个外键引用单亲实体 [英] ASP.NET MVC 4 Multiple Foreign Keys Referencing Single Parent Entity

查看:96
本文介绍了ASP.NET MVC 4多个外键引用单亲实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个ASP.NET MVC应用4,玩家可以根据自己的进攻,防御堪称并协助技能。进攻,防御和辅助上播放表的外键引用同一个查找表 - 评

I am trying to develop an ASP.NET MVC 4 application where players can be rated according to their Offence, Defence and Assist skills. Offence, Defence and Assist are foreign keys on Player table referencing the same lookup table - Rating.

我有以下父实体:

public class Rating
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Player> Players { get; set; }
}

和子实体:

public class Player
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int OffenceRatingId { get; set; }
    [ForeignKey("OffenceRatingId")]
    public virtual Rating OffenceRating { get; set; }

    public int DefenceRatingId { get; set; }
    [ForeignKey("DefenceRatingId")]
    public virtual Rating DefenceRating { get; set; }

    public int AssistRatingId { get; set; }
    [ForeignKey("AssistRatingId")]
    public virtual Rating AssistRating { get; set; }
}

建筑和脚手架都很好,但是当我运行应用程序,我得到以下错误:

Building and scaffolding went fine but when I run the app, I get the following error:

Introducing FOREIGN KEY constraint 'FK_dbo.Players_dbo.Ratings_DefenceRatingId' on table 'Players' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

我是新来的MVC和不知道我在这里失踪。任何帮助,这将大大AP preciated。谢谢你。

I am new to MVC and have no idea what I am missing here. Any help with this will be greatly appreciated. Thanks.

推荐答案

默认情况下,实体框架对删除会议级联。当两个实体的外键给对方,它会导致一个循环引用和级联删除上无法应用到实体。

By default, Entity Framework has a Cascade on Delete convention. When two entities have foreign keys to each other, it causes a circular reference and Cascade on Delete can't be applied to both entities.

的最简单的解决办法是删除<一href=\"http://geekswithblogs.net/danemorgridge/archive/2010/12/17/ef4-cpt5-$c$c-first-remove-cascading-deletes.aspx\">cascade在删除会,并通过案例的基础上应用它的情况。

The simplest solution is to remove the cascade on delete convention, and apply it on a case by case basis.

这篇关于ASP.NET MVC 4多个外键引用单亲实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆