LINQ context.SubmitChanges() does not add record to database

This never happens.

If you think that context.InsertOnSubmit(entity) is not adding the entity to your Context then you are doing something wrong.

In my case, I had code like this:


ProductContext context = new ProductContext();
Product product = new Product();
product.Id = productId;
product.Name = productName;
product.Description = productDescription;

context.InsertOnSubmit(product);
context.SubmitChanges();

Totally straightforward, but when I checked the database, the Product table did not contain the new Product record I had created above. But this problem only occurred on our Integration Test server, not on our Dev box (Why?). So began my journey of pain….

I ran the program a few times and noticed that my EntitySet, context.Products, was increasing in number each time I passed through the AddProduct method snippet above, but the database table, Product, as viewed through VS Server Explorer did not have the same number of records as the context. WHY?!!! (Some of you have guessed already.)

I started Googling for help: “InsertOnSubmit fails”; “SubmitChanges does not save”; “LINQ insanity coronary apoplexy” etc. Then spake Thrifty Dave, our noble Team Leader: “Perhaps the Connection Strings are different on the Test box,” suggested Dave between mouthfuls of remaindered Sushi. My pangs of internal anguish commenced immediately. This had the ring of truth as well as the scent of slightly fermented Wasabi paste.

I checked the Connection Strings: yep, the Context on Integration Test was pointing to “ProductTest” database whereas the actual database I was looking at was “ProductLive”. Too simple. There was NO problem with context.SubmitChanges(). I was just looking at the wrong database. *blush* (Respectfully please now imagine a luminous red object larger than the Hindenburg and covered in Wasabi paste).

Moral Of The Story

context.SubmitChanges() always works. If you think its not working, then its your fault. Look elsewehere. Maybe check your ConnectionStrings.

Plausible Deniability

The reported bug was in a Windows Service which utilizes the same database as the main application. On the IntegrationTest server, the Windows Service app.config had its ConnectionString set to “ProductLive” database due to an error in our deployment scripts. My colleague, who wrote the Windows Service told me that data was not being found by the Windows Service and HE was checking “ProductLive”, based on the (faulty) Windows Service app.config.

So I believed what I was told.
But, yes, I should have checked, and I should have realised quicker what was going on

Life Force Depletion: Classified.

About these ads

Tags: ,

12 Responses to “LINQ context.SubmitChanges() does not add record to database”

  1. gaurav Says:

    Hi Everybody,

    I solved the problem after 3 days of search work.

    Actually the problem with windows project is that when you save, all data saved in database copy created in bin/debug folder.

    So just attach the database file with server explorer, and the application again, you will the changes made in database

    • sk md Says:

      Hi, this problem is killing me. the database in my App_Data folder doesn,t get updated. but i see a copy of my database in bin/App_Data folder which seems to get updated when i run my application. how do i attach that mdf file to my application? how do i solve the problem?

  2. pwzeus Says:

    This is old but thought i will come and clear this up.

    Whenever your update or Submitchanges fails try puttin InsertOnSubmit() statment. This statment will give you detailed error.

    in my case I had very simple code.

    var single = dataContext.MySysTables.FirstOrDefault();
    single.DispatchIdIndex ++;
    single.ModifyDate = DateTime.Now;
    dataContext.SubmitChanges();

    This was not being updated. When I put a InsertOnSubmit(single) line than it gave me error that “data can not be inserted because table does not have Primary key”. This was my test table to i didnt have primary key. Once I added a new column and made a primary key than everything worked fine.

    peace

  3. Powershell: Variable Loses Value Outside Of Loop « Bite The Wax Tadpole Says:

    [...] Powershell: Variable Loses Value Outside Of Loop By baraholka1 This never happens either. [...]

  4. morteza Says:

    hello,
    but u r wrong in this post.
    u said that submitchanges does work n if not its our fault , but linq to sql has problem for submiting changes when u use a sql file database , it really does not save changes , no error , no exception , and of course my connection is correct n give true queries,n dont think about incorrect connection or wrong database.everything is ok , just submit
    when i changed my sql database file to an attached sql database it worked !

  5. Kobi Says:

    Here’s another dumb story:
    We had a wrapper that creates a Context per request. It turns out it didn’t work well, and returned a new Context each time…
    So, we had something like:

    Context.Profiles.InsertOnSubmit(profile);
    Context.SubmitChanges();

    Each one of theses lines created a new Context, so the changes were lost…

  6. Bobby Mac Says:

    “Each one of theses lines created a new Context, so the changes were lost…” —- I went through same dumb thing

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: