桌子上引进国外KEY约束可能会导致造成Database.SetInitializer无法工作周期或多个级联路径? [英] Introducing FOREIGN KEY constraint on table may cause cycles or multiple cascade paths causing Database.SetInitializer to not work?

查看:127
本文介绍了桌子上引进国外KEY约束可能会导致造成Database.SetInitializer无法工作周期或多个级联路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code-第一数据库伟大的工作。如果我做了一个改变我的数据库上下文数据库将下一次我启动的应用程序进行更新。但后来我加了一些模型到数据库中,当我重新启动我的申请得到这个错误:

My code-first database was working great. If I made a change to my database context the database would be updated the next time I started the application. But then I added some models to the database and got this error when I restarted my application:

对表'订单明细引进国外KEY约束'FK_OrderDetails_Orders_OrderId'可能会导致循环或多重级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
无法创建约束。见previous错误。

其中的一个奇怪的事情是,如果我再次启动应用起来不改变任何东西,然后我得到这个错误:

One of the weird things is that if I start the application up again without changing anything, I then get this error:

型号的兼容性无法进行检查,因为该数据库不包含模型元数据。型号兼容性只能检查使用$ C $或C首先code首先迁移。

要获得的第一个错误再次发生,我不得不删除我的.mdf和.ldf文件(数据库),并从我的修订历史记录副本只更换.mdf文件。

To get the first error to happen again, I have to delete my .mdf and .ldf files (the database) and replace just the .mdf file with a copy from my revision history.

在世界上为什么会出现这种情况?

参考:

我的Global.asax.cs文件中有这样的的Application_Start()方法中:

My Global.asax.cs file has this within the Application_Start() method:

Database.SetInitializer< EfDbContext>(新EfDbContextInitializer());

看起来像这样:

public class EfDbContextInitializer : DropCreateDatabaseIfModelChanges<EfDbContext>
{
    protected override void Seed(EfDbContext context)
    {
        var orders = new List<Order>
            {
                . . .
            };
        orders.ForEach(s => context.Orders.Add(s));
         . . . etc. . .
        context.SaveChanges();
    }
}

的Web.config我的连接字符串

&LT;添加名称=EFDbContext的connectionString =数据源= \\ SQLEX $ P $干燥综合征;集成Security=SSPI;database=pos;AttachDBFilename=|DataDirectory|pos.mdf;MultipleActiveResultSets=true;User实例=真的providerName =System.Data.SqlClient的/&GT;

最后,我的订单和订单明细车型(什么第一个错误是直接引用):

And, finally, my Order and OrderDetails models (what the first error is directly referencing):

public class Order
{
    public int OrderId { get; set; }

    public List<OrderDetail> OrderDetails { get; set; }

    public int EstablishmentId { get; set; }

    public virtual Establishment Establishment { get; set; }
}

public class OrderDetail
{
    public int OrderDetailId { get; set; }

    public int OrderId { get; set; }

    public int ProductId { get; set; }

    public int Quantity { get; set; }

    public decimal UnitPrice { get; set; }

    public virtual Product Product { get; set; }

    public virtual Order Order { get; set; }
}

更新/注:如果我注释掉公众诠释的OrderId {搞定;组; } 在我的OrderDetail类,在项目开始了罚款(虽然我没有得到添加所需能力的的OrderId (当然)。

Update / Note: If I comment out public int OrderId { get; set; } in my OrderDetail class, the project starts up fine (although I do not get the desired ability to add an OrderId (of course).

推荐答案

此问题是由一个可能的循环级联删除造成的。这可以多种形式发生,但它归结为通过被删除的记录两个或多个级联删除一次性规则。

This problem is caused by a possible cyclic cascading delete. This can happen in many forms, but it comes down to a record being deleted by two or more cascading delete rules in one time.

举例来说,假设你有父表及两个子表。然后,你还可以链接到两个子表的另一个表:

For example, lets say you have Parent table and two Child tables. Then you also have another table which is linked to both Child tables:

Parent
------
ParentId
Name

ChildOne
--------
ChildOneId
ParentId
Name

ChildTwo
--------
ChildTwoId
ParentId
Name

SharedChild
-----------
SharedChildId
ChildOneId
ChildTwoId
Name

当您删除父表中的记录,这有可能会删除级联到两个ChildOne的的ChildTwo。这两个删除就可以进一步级联SharedChild在这里我们得到的问题:两条路径试图删除SharedChild相同的记录

When you delete a record from the Parent table, it is possible this delete will cascade to both ChildOne and ChildTwo. These two deletes can then further cascade to SharedChild and here we get the problem: two paths trying to delete the same record from SharedChild.

我不是说你的情况是完全一样的,但这种或那种方式,你有类似的事情的东西。为了解决这个问题,你可以决定让一个分支被允许进一步向下级联链和prevent它的其它链。

I'm not saying your situation is exactly the same, but one way or another, you have something similar going on. To solve this, you can decide to let one branch be allowed to cascade further down the chain and prevent it in the other chain.

您收到此错误只有你运行你的应用程序,然后每次你删除的数据库首次究其原因,是因为(我相信)实体框架停止产生在发生错误的位置数据库,你只剩下一个不完整的数据库。这就是为什么你在其他情况下得到其他错误。

The reason you get this error only the first time you run your app and then every time you delete the database, is because (I believe) Entity Framework stops generating the database at the point the error occurs and you are left with an incomplete database. That's why you're getting the other error in the other situations.

如果您需要更多的帮助解决循环级联删除,你可能需要出示您的整个数据模型和它的关系。

If you need more help resolving the cyclic cascading delete, you will probably need to show your entire datamodel and its relations.

这篇关于桌子上引进国外KEY约束可能会导致造成Database.SetInitializer无法工作周期或多个级联路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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