10

My code is pretty simple:

Context.AddObject("EntitiesSetName", newObjectName);
Context.SaveChanges();

It worked fine, but just one time – the first one. That time, I interrupted my program by Shift+F5 after the SaveChanges() was traced. It was a debug process, so I manually removed a newly created record from a DB and ran a program again in the debug mode. But it does not work anymore – it “hangs” when SaveChanges() is being called.

Another strange thing that I see: If I write before addObject() and SaveChanges() are called something like:

var tempResult = (from mydbRecord in Context 
                  where Context.myKey == 123 
                  select mydbRecord.myKey).Count(); 
// 123 is the key value of the record that should be created before the program hangs.

tempResult will have the next value: 1.

So, it seems that the record is created (when the program hung) and now exists, but when I check the DB manually using other tools – it does not!

What do I do wrong? Is it some kind of cache issue or something else?

EDIT:

I've found a source of problem. It was not EF problem at all, but it's a problem of the tool that I use to control the database manually (Benthic).

My program falls into some kind of deadlock (when I call SaveChanges()) with the tool when the tool is connected into the same DB.

So, the problem is in the synchronization area, imho, so my question can be marked as solved.

user1622345
  • 111
  • 1
  • 5
  • Why don't you use Context.TypeClassName.Add(newObjectName); i suppose it's faster that the code you use – radu florescu Nov 03 '12 at 10:28
  • Do you mean Context.EntitySetName.AddObject(newObjectName)? It's just a wrapper for my method as far as I know – user1622345 Nov 03 '12 at 10:38
  • Yeah that is what I usually use. If you do a query on database entities you don't need to use saveChanges – radu florescu Nov 03 '12 at 10:50
  • The UOW has the entity you inserted in memory, but that is not yet commited to database. – radu florescu Nov 03 '12 at 10:52
  • I'm not sure that I understand you correctly, so how can I commit my new record into the database without SaveChanges()? I really need to add some new record there and AddObject() does not do that. – user1622345 Nov 03 '12 at 11:50
  • I thought you use Save Changes for query entities. So no, you should use SaveChanges to save an Added Object – radu florescu Nov 03 '12 at 14:23
  • I have same issue with Oracle / SQL Developer. If I perform some inserts in SQL Developers, but not commit or rollback, and then from my app I try to insert new and SaveChanges... then I get deadlock. Strange... I would expect some error back like in MSSQL 1205: "Process is a victim of a deadlock... BLAHBLAH!" – Learner Jan 27 '14 at 16:39
  • I have same issue with Oracle. Using .Net core EF – The Anh Nguyen May 18 '20 at 06:43

0 Answers0