Entity Framework Navigation Property with Id and Object fail

c# entity-framework entity-framework-6

Question

Problems occur while saving an entity where the navigation property is dynamically configured.

Here is a copy of a more intricate code.

namespace ConsoleAppEFAttaching
{
    public class MyContext : DbContext
    {
        public MyContext()
            : base("MyContextConnectionString")
        {
            base.Configuration.LazyLoadingEnabled = false;
            base.Configuration.AutoDetectChangesEnabled = false;
            base.Configuration.ProxyCreationEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Parent>();
            modelBuilder.Entity<Child>();
        }
    }
    public class Parent
    {
        public int Id { get; set; }
        public string NameParent { get; set; }

        public static Parent Create(int id)
        {
            return new Parent { Id = id };
        }
    }

    public class Child
    {
        private Parent theOnlyParent;
        public int Id { get; set; }
        public string NameChild { get; set; }

        public Parent TheOnlyParent {
            get { return Parent.Create(TheOnlyParentId); }
            set { TheOnlyParentId = value.Id; }
        }

        public int TheOnlyParentId { get; set; }
    }


    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Start create database");
            Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());
            Console.WriteLine("Start adding Parent");
            var p1 = new Parent {NameParent = "Test Parent Name#1"};
            int parentCreatedId;
            Console.WriteLine("Context");
            using (var context = new MyContext())
            {
                context.Set<Parent>().Add(p1);
                context.SaveChanges();
                parentCreatedId = p1.Id;
            }
            Console.WriteLine("Start adding a child from a different context");
            var c1 = new Child { NameChild= "Child #1" };
            c1.TheOnlyParentId = parentCreatedId;
            c1.TheOnlyParent = new Parent {Id = parentCreatedId};
            Console.WriteLine("Context");
            using (var context = new MyContext())
            {
                Console.WriteLine("*Change State Child");
                context.Entry(c1).State = EntityState.Added; // !!! Error : Conflicting changes to the role 'Child_TheOnlyParent_Target' of the relationship 'Child_TheOnlyParent' have been detected.
                Console.WriteLine("*Change State Child->Parent Navigability Property");
                context.Entry(c1.TheOnlyParent).State = EntityState.Detached;
                Console.WriteLine("*Save Changes");
                context.SaveChanges();
            }
            Console.WriteLine("End");
            Console.ReadLine();
        }
    }
}

When the Entry State is changed to Added, there is a problem. The mistakeConflicting changes to the role 'Child_TheOnlyParent_Target' of the relationship 'ConsoleAppEFAttaching.Child_TheOnlyParent' have been detected. raises.

The function is set and returned numerous times during the change of state, as shown if I place a Console.WriteLine inside the Child.TheOnlyParent property. I initially believed that the problem might be brought on by the fact that the returned object was different, but even if I simply create this object once (instantiate it just once and then return the same instance), the issue persists.

Should I not use the Parent. Develop in a child. TheOnlyParent is effective. But if we want to restrict Include for performance reasons, I want to apply our logic (with the Create method) to define the class by only the id.

Therefore, I have two questions: first, why does it call the Getter and Setter more than once when the state changes, and second, why do I have contradicting changes to the role?

1
1
7/2/2014 6:29:08 PM

Accepted Answer

ZZZ_tmp
0
7/3/2014 4:49:05 PM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow