删除然后保存然后添加VS删除然后保存然后创建新的DBContext然后添加然后保存... [英] delete then save then add VS delete then save then create new DBContext then add then save...

查看:64
本文介绍了删除然后保存然后添加VS删除然后保存然后创建新的DBContext然后添加然后保存...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在一些旧代码中遇到了一个奇怪的错误。

I've got a weird bug in some old code.

代码:

如果数据存在于日期X删除,SaveChanges()*(见下文),添加,SaveChanges()

if data exists for date X delete, SaveChanges() *(see below), add, SaveChanges()

用户说这是工作和测试...我无法看到这是如何工作的,因为我每次都可以重复这个bug。它具有的效果是用户"加载数据"。 (没问题),但如果他们需要重新加载系统"加载数据",
说好,但只有删除才会发生。所以数据消失但没有抛出错误。

users say this was working and tested... I can't see how this ever worked as I can repeat the bug every time. The effect its having is that the users "load the data" (no problem), but then if they ever need to reload the system "load the data" says OK, but only the delete happens. So the data disappears but no error is thrown.

如果没有该日期的数据那么会发生什么呢,但是如果有现有数据,那么删除就会发生但是添加永远不会完成 - 日志说它做它的东西,但它太快,~450ms,而不是大约13000,当它
实际工作。

what happens is if there is no data for that date then its fine, but if there is existing data, then the delete is occuring but the add never completes - the logs say its doing its thing but its too quick, ~450ms, as opposed to around 13000 when it actually works.

*问题是该错误似乎是在"中间"。 saveChanges(),它实际上调用了一个名为"discardAndRecreateConnection()"的函数。这样做:

* the catch is that the bug appears to be in teh "middle" saveChanges() where its actually calling a funciton called "discardAndRecreateConnection()" which does this:

db = discardAndRecreateConnection(db);

db = discardAndRecreateConnection(db);

其中

DbContext discardAndRecreateConnection(DbContext old )

{var newDb = CreateContext(old.ConnectionString);

返回newDb;

}

DbContext discardAndRecreateConnection(DbContext old)
{var newDb = CreateContext(old.ConnectionString);
return newDb;
}

我的问题是:

a)删除和重新添加数据是否有必要执行上述操作?如果是这样你怎么做对......?

a)with the deleting and re-adding of data is it necessary to do something like the above? if so how do you do it RIGHT...?!

b)为什么这似乎无声地失败 - 发生的事情是它没有错误但是"添加"跟随丢弃adn重新创建永远不会进入数据库调用...

b)why does this appear to fail silently - what is going on that it doesn't error but the "adds" that follow the discard adn recreate never make it to a DB call...

c)如果你只是放弃"丢弃"并且纯粹做了一个"delete / savechanges()/ add / savechanges()"你是否愿意遇到任何问题?

c) if you simply ditch the "discard" and purely did a "delete/savechanges()/add/savechanges()" are you goign to run into any issues?

****************************** **

更新 - 在更改历史记录中找到关于此代码存在的原因的评论。

********************************
UPDATE - Found a comment in the change history as to why this code exists.

随着大量替换数据条目,内存将会爆炸,所以看起来像"丢弃并重新创建"是试图强迫EF释放一些记忆......不确定这是否有帮助

With a large number of replacing data entries the memory would blow out, so looks like the "discard and recreate" was to attempt to force EF to release some memory... not sure if that helps at all

******************* *************

********************************

- 确定我是noJedi但是没有理由停止尝试让东西漂浮! -

- sure I'm noJedi but that's no reason to stop trying to make stuff levitate! -

推荐答案

嗨Nojedi,

Hi Nojedi,

>> a)删除和重新添加数据是否有必要执行上述操作?如果是这样你怎么做对......?

>> a)with the deleting and re-adding of data is it necessary to do something like the above? if so how do you do it RIGHT...?!

b)为什么这似乎无声地失败 - 发生的事情是它没有错误但是"添加"跟随丢弃adn重新创建永远不会进入数据库调用...

b)why does this appear to fail silently - what is going on that it doesn't error but the "adds" that follow the discard adn recreate never make it to a DB call...

c)如果你只是放弃"丢弃"并且纯粹做了一个"delete / savechanges()/ add / savechanges()"您是否愿意遇到任何问题?

c) if you simply ditch the "discard" and purely did a "delete/savechanges()/add/savechanges()" are you goign to run into any issues?

您是否尝试通过使用相同的连接字符串重新创建新的数据库上下文来解决此问题?我认为你是在错误的方向来解决这个问题。如果你想找到为什么新数据不能插入到db中,我建议你使用SQL Server Profiler监视执行的sql语句
你的应用。它可以记录EF发送的插入和删除语句,您将了解有关此问题的更多信息。

Are you trying to solve this problem by recreate a new db context using the same connection string? I think you are on the wrong direction to solve this problem. If you want to find why new data cannot insert into db, I would recommend you use SQL Server Profiler to monitor the executed sql statement which called by your app. It can log insert and delete statement sent by EF and you will know more about this issue.

查看有关该探查器的更多信息,
https://msdn.microsoft.com/en-us/library/ms181091.aspx?f=255&MSPPError=-2147217396
,试试并发布更实用的信息关于你的问题。

See more info about the profiler, https://msdn.microsoft.com/en-us/library/ms181091.aspx?f=255&MSPPError=-2147217396, try it and post more useful info about your problem.

如果你不知道如何处理这个工具,你可以发布一个重复样本,我会继续努力让你知道我的结果。

If you don't know how to do with the tool, can you post a repro sample, I will work on and let you know my result.

问候,


这篇关于删除然后保存然后添加VS删除然后保存然后创建新的DBContext然后添加然后保存...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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