ADO.NET 实体中的 ObjectContext.SaveChanges() 问题
当使用映射到存储过程的视图来更新/插入/删除数据时,我遇到以下问题:
我有一个名为 tbCurrencyRates 的表,在此表中我将每种货币的货币汇率与另一种货币相关,但如果我更新美元/欧元货币对 我还必须更新欧元/美元货币对的值。
我使用直接映射到 ADO.NET 实体 4.0 中的 tbCurrencyRates 表,其中框架生成更新/插入/删除记录所需的查询。我将一个新的对象上下文(未加载任何实体)放入 CurrencyRates 集中,然后传递 USD/EUR 汇率的CurrencyRate 对象来执行以下操作:
- 我查询(使用 linq)欧元/美元货币对。
- 我更新其速率
- 并保存更改。
- 一切都很顺利,直到这里
下一步,我附加传递的CurrencyRate(美元/欧元汇率)并再次调用SaveChanges。
使用直接访问表,一切顺利,但是当我用视图替换表时(我添加了插入/更新/删除所需的所有存储过程映射),框架抛出一个异常,指出附加的CurrencyRate(对于美元/美元)欧元汇率)已经存在。
请注意,如果我使用表而不是视图,一切都会顺利进行。仅当我使用视图以及第二次调用 SaveChanges 时(尽管我使用新的对象上下文),才会发生此错误。
问题是使用表和视图与 ADO.NET 实体有什么区别,框架在执行更新操作时是否会查询数据库中的所有实体(如果使用视图访问数据)。
这是代码:
using (ICurrenciesRepository repository = NewCurrenciesRepository())
{
SetLastChangedDate(rate);
CurrencyRate alternative = this.ProcessChangedCurrencyRate(rate, repository); //This performs the update correctly
updates.Add(repository.UpdateCurrencyRate(rate)); //this fails to attach and update the rate object although I use a new repository and the rate was gotten de-attached from another object context.
if (alternative != null)
updates.Add(alternative);
return updates;
}
I have the following problem when using a view mapped to stored procedures to update/insert/delete data:
I have a table called tbCurrenciesRates, in this table I put currencies rates for each currency in relation with another currency but if I update the rate of USD/Euro currencies pair I must update the value of Euro/USD pairs as well.
I was using direct mapping to tbCurrenciesRates table in ADO.NET entities 4.0, where the framework generated the queries required to update/insert/delete record. I make a new object context (where no entities are loaded) into the CurrenciesRates set, then I pass a CurrencyRate object for USD/Euro rate to perform the following:
- I query (using linq) for the Euro/USD pair.
- I update its rate
- I save changes.
- Everything goes well till here
Next, I attach the passed CurrencyRate (for USD/Euro rate) and call SaveChanges again.
Using direct access to a table, everything goes well, but when I replaced the table with a view (I added all required stored procedures mapping for insert/update/delete), the framework throws an exception saying that the attached CurrencyRate (for USD/Euro rate) already exists.
Note that if I use the a table instead a view everything goes well. This error happens only when I use a view and when I call SaveChanges for the second time although I use a new object context.
The question is what is the difference between using a table and view with ADO.NET entities, does the framework queries all entities in the database when performing an update operation in case it was accessing data using a view.
Here is the code:
using (ICurrenciesRepository repository = NewCurrenciesRepository())
{
SetLastChangedDate(rate);
CurrencyRate alternative = this.ProcessChangedCurrencyRate(rate, repository); //This performs the update correctly
updates.Add(repository.UpdateCurrencyRate(rate)); //this fails to attach and update the rate object although I use a new repository and the rate was gotten de-attached from another object context.
if (alternative != null)
updates.Add(alternative);
return updates;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了。由于某种原因,当我从表切换到视图时,EF 无法重新生成代码。我删除了存储过程和实体,然后通过数据库中的更新模型再次添加它们,之后一切都按预期进行。
Solved. For some reason EF failed to regenerate code when I switched from table to view. I deleted the stored procedures and the entity, then I added them again via update model from database, after this everything worked as expected.