开发者

Problems with ObjectContext.SaveChanges() in ADO.NET Entities

I have the following problem when using a view mapped to stored procedures to update/insert/delete data:

I have a table called tbCurren开发者_JS百科ciesRates, 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;

            }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜