locked
Context.SaveChanges() method not working RRS feed

  • Question

  • Unable to make "context.savechanges()" method work. The following is my code. 

    Dim dbcontext As New Model1Container

            Dim employee As New emp

            employee.empid = 1

            employee.empname = "some name"

            employee.empaddress = "some address"

            dbcontext.emps.AddObject(employee)

     

            Try

                dbcontext.SaveChanges()

            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

     

    After execution of this code, the data is not saved to the emp table. I am not getting any error on executing this codes. Runs fine but it does not save. I am not sure what I am doing wrong. Please help. 

     

     

    Wednesday, January 18, 2012 10:22 PM

Answers

  • Hi NotoriousLegend,

    The LINQ query can be write like this

    var query = from tr in context.eposdata.dbo.transactionrecords group tr by tr.ProductSubCategory into temp select new
    {
        sum = temp.Sum(t=>t.SubTotal),
        subCategory = temp.key
    };

     

    Best Regards
    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Allen_MSDN Thursday, February 2, 2012 2:16 AM
    Wednesday, January 25, 2012 4:19 AM

All replies

  • Hi NotoriousLegend,

    Welcome to MSDN Forum.

    I didn't find anything wrong with the code, pleaes add a breakpoint to trace the employee's state.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 2:56 AM
  • Hi, 

    The state does not change. I have no clue on how to manually change the state. Any ideas or examples ?

    Please check the attached screen shot which says more about objectstatemanager.

     

    Saturday, January 21, 2012 2:09 PM
  • The following is the state of employee entity. 

     

    in dim statement  - detached{1}

    in addtoemps statement  - added{4}

    after savechanges statement  - unchanged{2}

     

    but still no changes to the table. 

    Saturday, January 21, 2012 2:15 PM
  • Tried LINQ to SQL as well with the following code, 

     

            Dim db As New AppDataContext

     

            Dim employee As New emp

            employee.empname = "some name"

            employee.empaddress = "some address"

            db.emps.InsertOnSubmit(employee)

            db.SubmitChanges()

     

    This too did not work. Guess there is some settings I need to change in VS or SQL Server. I am not sure what. Any ideas ?

    Saturday, January 21, 2012 2:28 PM
  • Hi NotoriousLegend,

    The state of the entity is correct, I'm not sure why the state of the entity is added when you have added to the context, but the database doesn't update. If possible, please send the demo to v-alll@microsoft.com , I'll help you to test it.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 3:01 AM
  • Strange  problem. I formatted the system thinking it is related with some kind of installation bug. But no luck still facing the same problem. 

    However when I publish and install it in the same system, the data is saved to database. But not possible to do the same while writing the code in VB. Very strange problem. Currently I am managing by publishing the app and testing it. 

    Hoping someone will find the problem and state the solution soon. 

    Thanks for your help. 

     

    Do you have any idea on transforming the following SQL query to linq to entities query ?

    select sum(tr.SubTotal),tr.ProductSubCategory from eposdata.dbo.transactionrecords as tr

    group by tr.ProductSubCategory

    Thanks.

    Tuesday, January 24, 2012 12:46 PM
  • Hi NotoriousLegend,

    The LINQ query can be write like this

    var query = from tr in context.eposdata.dbo.transactionrecords group tr by tr.ProductSubCategory into temp select new
    {
        sum = temp.Sum(t=>t.SubTotal),
        subCategory = temp.key
    };

     

    Best Regards
    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Allen_MSDN Thursday, February 2, 2012 2:16 AM
    Wednesday, January 25, 2012 4:19 AM