ADO.NET 实体中的 ObjectContext.SaveChanges() 问题

发布于 2024-10-22 06:55:01 字数 1362 浏览 0 评论 0原文

当使用映射到存储过程的视图来更新/插入/删除数据时,我遇到以下问题:

我有一个名为 tbCurrencyRates 的表,在此表中我将每种货币的货币汇率与另一种货币相关,但如果我更新美元/欧元货币对 我还必须更新欧元/美​​元货币对的值。

我使用直接映射到 ADO.NET 实体 4.0 中的 tbCurrencyRates 表,其中框架生成更新/插入/删除记录所需的查询。我将一个新的对象上下文(未加载任何实体)放入 CurrencyRates 集中,然后传递 USD/EUR 汇率的CurrencyRate 对象来执行以下操作:

  1. 我查询(使用 linq)欧元/美元货币对。
  2. 我更新其速率
  3. 并保存更改。
  4. 一切都很顺利,直到这里

下一步,我附加传递的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:

  1. I query (using linq) for the Euro/USD pair.
  2. I update its rate
  3. I save changes.
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许一世地老天荒 2024-10-29 06:55:01

解决了。由于某种原因,当我从表切换到视图时,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文