Entity framework returns entity which doesn't exist in database
Posted by: dupond martin
Date: September 29, 2015 06:43AM

I got a very strange behavior with EF6. I have an entity which insertion failed, but when i enumerate the entities, the first time after that, I retrieve the entity, even if i create a new context
Here a sample code in C#


MyDbContext ctx = new MyDbContext();
Category category = new Category()
{
Name = "TEST INSERTION FAILED",
TranslationCategories = new List<TranslationCategory>()
{
new TranslationCategory()
{
LangId = "FR",
Name= "FR translation"
}
}
};
try
{
ctx.Categories.Add(category);
await ctx.SaveChangesAsync();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
// throw exception because "TranslationCategory" table doesn't exist in DB
}
MyDbContext ctx2 = new MyDbContext();
foreach(var o in ctx2.Categories.ToList())
{
Console.WriteLine(o.Name);
//output : "TEST INSERTION FAILED" + categories stored in DB
// Why category "TEST INSERTION FAILED" in ctx2.Categories ?
}
Console.WriteLine("****************");
foreach (var o in ctx2.Categories.ToList())
{
Console.WriteLine(o.Name);
// ouput : only categories stored in DB
}


The same code, with same schema in sql server, doesn't reproduce this bug.

Options: ReplyQuote


Subject
Written By
Posted
Entity framework returns entity which doesn't exist in database
September 29, 2015 06:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.