实体框架代码优先并发 [英] Entity framework code-first concurrency

查看:110
本文介绍了实体框架代码优先并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用EntityFramework 6和代码优先模型撕掉我的头发并考虑回到NHibernate这一时刻,使用MySQL数据库。



基本上,我有一个这样的模型:



  public   class  MyModel 
{
[Key,Column(< span class =code-string> ID)]
public int ID { get ; set ; }

[列( ADDED)]
< span class =code-keyword> public 添加日期时间{ get ; set ; }

[ForeignKey( MySecondModelID)]
< span class =code-keyword> public virtual MySecondModel { get ; set ; }

[Column( MYSECONDMODELID)]
< span class =code-keyword> public int ? MySecondModelID { get ; set ; }
}





表格列MYSECONDMODELID可以为空,因此不需要记录。如果提供的话,它是一个可选的子项。



我实际上是在创建一个新的模型实例并尝试将其保存到数据库中,如下所示:使用( var <<$ class> / span> context = new MyDbContext())
{
var model = new MyModel()
{
已添加= DateTime.Now,
MySecondModel = null
};

context.MyModels.Add(model);
context.SaveChanges();
}





但是我一直收到同样的异常,并显示错误消息:



存储更新,插入或删除语句影响了意外的行数(0)。实体可能已被修改或删除,因为实体已加载。



显然,我已经用这个特定错误搜索了爱母亲的垃圾,每一个结果都会让我采用相同的方法来解决问题:在尝试保存更改之前刷新当前上下文。



所以,我做了以下几点:



 尝试 
{
context.SaveChanges();
}
catch (异常例外)
{
var context =((IObjectContextAdapter)context).ObjectContext;
context.Refresh(RefreshMode.StoreWins,model);
context.SaveChanges();
}





但总会引发相同的异常。无论我做什么,我都无法摆脱那个错误。有没有办法在EntityFramework中禁用并发检查?我觉得如果我可以取消支票,我的大多数问题都会消失。



我应该指出,我已经通过谷歌搜索结果提出了大约8种不同的解决方案。这些包括上面的上下文刷新,以及将异常的OriginalValues设置为当前值。



任何人都可以指出我可能出错的地方吗?


谢谢!



我尝试过:



保存前刷新数据库上下文

保存前将当前实体OriginalValues设置为CurrentValues

用于搜索问题的原因最多10个页面深入到Google。

解决方案

经过大量搜索并对在底层EntityFramework代码中执行的查询执行一些诊断后,我注意到新记录始终是正在为MySecondModel类插入。这让我意识到异常被抛出,因为每次保存MyModel类时数据库都保存了一个新的MySecondModel行。



为了避免这个问题,我现在在每个保存操作上附加现有的MySecondModel行:



 context.MyModels.Add(model); 
context.MySecondModels.Attach(model.MySecondModel);
context.SaveChanges();





这可以防止EntityFramework每次都需要保存新记录。


Hi guys,

I'm having a "tearing my hair out and considering moving back to NHibernate" moment with EntityFramework 6 and a code-first model, with a MySQL database.

Essentially, I have a model as such:

public class MyModel
{
     [Key, Column("ID")]
     public int ID { get; set; }

     [Column("ADDED")]
     public DateTime Added { get; set; }

     [ForeignKey("MySecondModelID")]
     public virtual MySecondModel { get; set; }

     [Column("MYSECONDMODELID")]
     public int? MySecondModelID { get; set; }
}



The table column "MYSECONDMODELID" is nullable, therefore the record is not required. It's an optional child to be included if provided.

I'm essentially creating a new instance of the model and attempting to save it to the database like so:

using (var context = new MyDbContext())
{
     var model = new MyModel()
     {
          Added = DateTime.Now,
          MySecondModel = null
     };

     context.MyModels.Add(model);
     context.SaveChanges();
}



But I keep receiving the same exception with the error message:

"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded."

Obviously I have Googled the mother-loving junk out of this particular error, and every single result leads me to the same methodology to cure the problem: refresh the current context before attempting to save the changes.

So, this I have done by doing the following:

try
{
     context.SaveChanges();
}
catch (Exception exception)
{
     var context = ((IObjectContextAdapter)context).ObjectContext;
     context.Refresh(RefreshMode.StoreWins, model);
     context.SaveChanges();
}



But the same exception is always raised. No matter what I do, I cannot get that error to go away. Is there a way to disable concurrency checking within EntityFramework? I sense that most of my problems would dissipate if I could eliminate the checks.

I should point out that I have tried around 8 different solutions suggested through Google results. These include the above context refresh, as well as setting the exception's OriginalValues to the current values.

Can anybody possibly point out where I might be going wrong?

Thanks!

What I have tried:

Refreshing the database context before saving
Setting the current entity OriginalValues to CurrentValues before saving
Googled for the cause of the issue up to 10 pages deep into Google.

解决方案

After much searching and after performing some diagnostics on the queries executing in the under-lying EntityFramework code, I noticed that a new record was always being inserted for the MySecondModel class. This lead me to realise that the exception was being thrown because the database had saved a new MySecondModel row every time the MyModel class was saved.

In order to circumvent this problem, I now attach the existing MySecondModel row on every save operation:

context.MyModels.Add(model);
context.MySecondModels.Attach(model.MySecondModel);
context.SaveChanges();



This prevents the EntityFramework from needing to save a new record each time.


这篇关于实体框架代码优先并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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