title: Entity Framework 4 Entity with EntityState of Unchanged firing update
body: <p>I am using EF 4, mapping all CUD operations for my entities using sprocs.  </p>
  
  <p>I have two tables, ADDRESS and PERSON.  A PERSON can have multiple ADDRESS associated with them.</p>
  
  <p>Here is the code I am running:</p>
  
  <pre><code>Person person = (from p in context.People
                               where p.PersonUID == 1
                               select p).FirstOrDefault();
  
  Address address = (from a in context.Addresses
                                 where a.AddressUID == 51
                                 select a).FirstOrDefault();
  
  address.AddressLn2 = "Test";
  
  context.SaveChanges();
  </code></pre>
  
  <p>The Address being updated is associated with the Person I am retrieveing - although they are not explicitly linked in any way in the code.  When the context.SaveChanges() executes not only does the Update sproc for my Address entity get fired (like you would expect), but so does the Update sproc for the Person entity - even though you can see there was no change made to the Person entity.  </p>
  
  <p>When I check the EntityState of both objects before the context.SaveChanges() call I see that my Address entity has an EntityState of "Modified" and my Person enity has an EntityState of "Unchanged".</p>
  
  <p>Why is the Update sproc being called for the Person entity?  Is there a setting of some sort that I can set to prevent this from happening?</p>
  
  <hr>
  
  <p>I created a second project to make sure the issue was not happening because of something in my current project environment. </p>
  
  <p>First, I created a new database that contains an Order and OrderDetail table.  They have a foreign key between them so that Order can have more than one OrderDetail associated with it.  I also created "after Update" DB triggers on the Order and OrderDetail tables that update a DateTime field when a record is updated.</p>
  
  <p>Second, I created a simple WPF application and created a ADO.NET Entity Model that was generated from my database.</p>
  
  <p>Third, I added code to my class constructor as follows:</p>
  
  <pre><code>public partial class MainWindow : Window
    {
      public MainWindow()
      {
        InitializeComponent();
  
        MyEntities context = new MyEntities();
  
        Order order = (from o in context.Orders
                select o).FirstOrDefault();
  
        OrderDetail orderDetail = (from d in order.OrderDetails
                      select d).FirstOrDefault();
  
        orderDetail.Qty = 7;
  
        context.SaveChanges();
      }
    }
  </code></pre>
  
  <p>I ran the program without doing any function mapping for my Order and OrderDetail objects.  The result was exactly what I would expect to see, the OrderDetail record is updated to have a Qty of 7 and the UpdateDateTime field is populated with the date and time the update occurred.  No changes to my the Order record which means no update occurred.</p>
  
  <p>I then create stored procedures to handle the update of Order and OrderDetail tables in the DB.  They don't do anything special, just accept parameters for every column in the table and then set each field equal to the associated parameter.  I then mapped those stored procedures to my Model objects using the Mapping Details (Map Entity to Functions) window.</p>
  
  <p>After doing the mapping, I ran the program.  As a result I observed the expected behavior of seeing updates to the OrderDetail table, but in addition the Order table "after Update" trigger had fired and the UdateDateTime field was set to the date and time the update occurred - which I would not expect. </p>
  
  <p>Does anyone know of a way to utilize stored procedures for all Insert, Update and Delete operations and not have the updates cascade up to related entities?</p>