none
How does DbChangeTracker.HasChanges work RRS feed

  • Question

  • I am looking for an example. I need to know if my record has been updated in my application.

    I use entity Framework 6.1. I don't know which object to hang this method on.


    Certified Geek

    Wednesday, May 28, 2014 12:08 PM

Answers

  • using (var db = new Model1())
    {
        if (db.ChangeTracker.HasChanges())
        {
            Console.WriteLine("Something has changed");
        }
    }
    


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by Arne MN Wednesday, May 28, 2014 1:36 PM
    • Unmarked as answer by Arne MN Wednesday, May 28, 2014 2:39 PM
    • Marked as answer by Arne MN Wednesday, May 28, 2014 2:55 PM
    Wednesday, May 28, 2014 12:25 PM

All replies

  • using (var db = new Model1())
    {
        if (db.ChangeTracker.HasChanges())
        {
            Console.WriteLine("Something has changed");
        }
    }
    


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    • Marked as answer by Arne MN Wednesday, May 28, 2014 1:36 PM
    • Unmarked as answer by Arne MN Wednesday, May 28, 2014 2:39 PM
    • Marked as answer by Arne MN Wednesday, May 28, 2014 2:55 PM
    Wednesday, May 28, 2014 12:25 PM
  • What is Model1 ?

    Certified Geek

    Wednesday, May 28, 2014 12:29 PM
  • That is my derived DbContext

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, May 28, 2014 12:32 PM
  • I tried it on my derived DBContext

    context.DbChangeTracker.HasChanges(); does not compile.


    Certified Geek

    Wednesday, May 28, 2014 12:47 PM
  • Works on my PC. And you use  EF 6?

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, May 28, 2014 12:50 PM
  • EF 6.1

    Certified Geek

    Wednesday, May 28, 2014 12:51 PM
  • And "context" is DbContext and not ObjectContext?

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, May 28, 2014 12:58 PM
  • Pls provide repro code...

    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Wednesday, May 28, 2014 12:58 PM
  • Yes I use DBContext.

    Certified Geek

    Wednesday, May 28, 2014 1:01 PM
  • What is the exception and inner exception messages you are getting?

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, May 28, 2014 1:34 PM
  • changed
    Model1 db = new Model1())
    to
    var db = new Model1())

    Now my code compiles. (I hope it behaves)


    Certified Geek

    Wednesday, May 28, 2014 1:36 PM
  • Fernando,

    No exception, only compile error.


    Certified Geek

    Wednesday, May 28, 2014 1:37 PM