Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: Bug or normal work EF 6.1.1?

$
0
0
Hi,guys. I use EF 6.1.1 and EF 6.1.2-beta2

I have some code :
        public abstract class Base
        {
            public Guid Id { get; set; }
        }

        public class Employee : Base
        {
            public Address Address { get; set; }
        }

        public class Address : Base
        {
            
        }

        public sealed class TestContext : DbContext
        {
            public DbSet<Employee> Employees { get { return Set<Employee>(); } }
            public DbSet<Address> Addresses { get { return Set<Address>(); } } 
        }

        using (var ct = new TestContext())
            {
                var address1 = new Address() {Id = Guid.NewGuid()};
                var address2 = new Address();
                address2.Id = address1.Id;


                var employee1 = new Employee() {Id = Guid.NewGuid(), Address = address1};
                var employee2 = new Employee() { Id = Guid.NewGuid(),Address = address2};

                ct.Employees.Attach(employee1);
                ct.Employees.Attach(employee2);

                ct.SaveChanges();
            }
When it executing , on second Attach operation, throwing exception :
Additional information: Attaching an entity of type 'ConsoleApplication1.Program+Address' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
I understand what happens into deeps EF, but i don't know this bug or EF normal working, please help me understand.

On real project this use into disconnected context.

P.S On EF 6.0.2 it code is work without exception.

Viewing all articles
Browse latest Browse all 1793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>