locked
Navigation property object gets updated automatically while updating the main entity/object in EF4 RRS feed

  • Question

  • Hi,

    We are working on a WPF application and using EF4. While updating an object, the object in it's navigation properties also gets updated even though we do not make any change in the Navigation property object. EF4 create an update query for the Navigation property object and update it's timestamp attribute. e.g. we have main entity 'Session' and its navigation entity is 'Context'. While updating 'Session', 'Context' also gets updated automatically even though we didn't make any change in the 'Context' object. Kindly let us know if there is a way to stop updating the navigation property object if the object has not been altered. Your help will be highly appreciated. Thanks

    Monday, July 2, 2012 8:51 AM

Answers

  • Hi yogeshnegi,

    Welcome to MSDN Forum.

    I've tested, when I update an object, the navigation property doesn't update. Below is my test code and the update T-SQL which generated by EF.

    class Program
        {
            static void Main(string[] args)
            {
                using (Context context = new Context())
                {
                    Person p = context.personSet.First();
                    p.name = "ass";
                    context.SaveChanges();
                }
            }
        }
    
        class Person
        {
            public int id { get; set; }
            public string name { get; set; }
            public PersonDetail detail { get; set; }
        }
    
        class PersonDetail
        {
            public int id { get; set; }
            public bool sex{get;set;}
            public int age { get; set; }
            public Person person { get; set; }
        }
    
        class Context : DbContext
        {
            public DbSet<Person> personSet { get; set; }
            public PersonDetail detailSet { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Person>().HasRequired(x => x.detail).WithRequiredPrincipal(x => x.person);
            }
        }

    T-SQL from SQL Server Profiler

    exec sp_executesql N'update [dbo].[People]
    set [name] = @0
    where ([id] = @1)
    ',N'@0 nvarchar(max) ,@1 int',@0=N'ass',@1=1

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Allen_MSDN Monday, July 9, 2012 2:30 AM
    Wednesday, July 4, 2012 7:26 AM