none
Why isn't ctx.SaveChanges saving anything? RRS feed

  • Question

  • Hi all,

    Ok, another minor problem.  I thought I had this code working.  So I tried to add an event handler for onSavingChanges.  But on subsequent testing, I found that things weren't saving.  So I commented out the code that loads those events (in the template and the class.)  I ran it and checked the SQL server and sure enough, nothing was saved.

    I am sure it's something very simple.  Here is the code I am executing:

    protected int getCtr()
    		{
    			int ctr = 0;
    			using (myEntities ctx = new myEntities(this.myconfig.getEfConnectionString() ))
    			{
    				// Get the next counter & increment it
    				var loIndicy = ctx.indecies.Where(o => o.p_key == 1).First();
    				loIndicy.counter++;<br/>				// Ok, try to save it
    				var loResults = ctx.SaveChanges();
    				// Did it save?
    				if (loResults != 0)
    				{
    					ctr = loIndicy.counter ;
    				}
    			}
    			return ctr;
    		} 
    

    When this executs, it does get the current value from the server.  But after the SaveChanges executes, the SQL server is unchnaged and the return value is 0.  I have tried to look at the entity that was modified to see if the objectState has changed and it doesn't appear to reflect the increment.

    And yes, there is more to the real method, but I reduced (and tested) it at the bare minimum to demonstrate the problem.

    Any ideas?

    me


    FletcherJ
    Monday, August 29, 2011 8:29 PM

Answers

  • Larcolais,

    If the query.id field is the primary key, I would hope that it would fail if you tried to change it. 

    In my case, I search on the primary key (p_key) as in "var loIndicy = ctx.indecies.Where(o => o.p_key == 1).First()" and then use the counter value from the entity to get a new counter value (loIndicy.counter++).

    In this case, I am just modifying a regular field in the table.  It doesn't even have an index on it.

    Anyway, I have rebuilt the entire edmx project, without any special templates, and that is now working.

    And I have researched the objectStateManager and that at least is letting me know the entity state.  At some point, I may try to go back to the original project to see if I can track down why it was not working, but the new project seems to work as desired.

    Thanks,

    me


    FletcherJ
    • Marked as answer by FletcherJ Thursday, September 1, 2011 6:45 PM
    Thursday, September 1, 2011 6:43 PM

All replies

  •  

    Does the table indecies have a Primary Key? if not it will not update.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, August 30, 2011 3:43 AM
  • Are there any changes being recorded in ctx.ChangeTracker? If so, you can check the queries making it to the database using the SQL Profiler.
    Tuesday, August 30, 2011 4:06 AM
  • Fernando,

    Yes, there is a primary key and the code did work at one point....  There doesn't seem to be anything wrong, so I have to assume that there is something else broken.  But I don't know what to check for to start debugging why it isn't saving.

    Any other ideas?

    Thanks,

    me


    FletcherJ
    Tuesday, August 30, 2011 5:19 PM
  • Narayan,

    I am using a local SQL Express.  I have something named "Data Profile Viewer" but that want's me to load a file from the disk.  I don't think it is the SQL Profiler to which you refer.

    How do I get to the ctx.ChangeTracker? When I try to reference that, the IDE doesn't seem to recognize it as a property or method.

    Thanks,

    me


    FletcherJ
    Tuesday, August 30, 2011 5:25 PM
  • Hello,

    I made a test on my side. It looks OK, but didn't work on the primary key and was thrown exception. Here's my test result. Hope it helps.

    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main(string[] args)
        {
          int ctr = 0;
          using (XueyunEntities ctx = new XueyunEntities())
          {
            var query = ctx.Parents.Where(p => p.Price == 90).First();
            query.id++;
            var result = ctx.SaveChanges();
            if (result != 0)
            {
              ctr = query.Price;
            }
              
          }
    
          Console.ReadLine();
        }
      }
    }
    
    SQL Profiles:
    --var query = ctx.Parents.Where(p => p.Price == 90).First();
    
    SELECT TOP (1) 
    [Extent1].[Price] AS [Price], 
    [Extent1].[startDate] AS [startDate], 
    [Extent1].[endDate] AS [endDate], 
    [Extent1].[id] AS [id], 
    [Extent1].[ParentName] AS [ParentName]
    FROM [dbo].[Parent] AS [Extent1]
    WHERE 90 = [Extent1].[Price]
    
    --var result = ctx.SaveChanges();
    
    exec sp_executesql N'update [dbo].[Parent]
    set [startDate] = @0
    where ([id] = @1)
    ',N'@0 int,@1 int',@0=3,@1=1
    

    Like you've seen, I didn't find any changes after SaveChanges method.

    Thanks,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 1, 2011 2:29 AM
  • Larcolais,

    If the query.id field is the primary key, I would hope that it would fail if you tried to change it. 

    In my case, I search on the primary key (p_key) as in "var loIndicy = ctx.indecies.Where(o => o.p_key == 1).First()" and then use the counter value from the entity to get a new counter value (loIndicy.counter++).

    In this case, I am just modifying a regular field in the table.  It doesn't even have an index on it.

    Anyway, I have rebuilt the entire edmx project, without any special templates, and that is now working.

    And I have researched the objectStateManager and that at least is letting me know the entity state.  At some point, I may try to go back to the original project to see if I can track down why it was not working, but the new project seems to work as desired.

    Thanks,

    me


    FletcherJ
    • Marked as answer by FletcherJ Thursday, September 1, 2011 6:45 PM
    Thursday, September 1, 2011 6:43 PM