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

New Post: Composite Key in derived class.

$
0
0
Hello,

I would know more about composite key when using inheritence.

Look at my problem :
        public class User
        {
            public User()
            {
                Contacts = new List<ContactUser>();
            }

            [Key]
            public int ID { get; set; }

            public virtual string Nom { get; set; }

            public virtual List<ContactUser> Contacts { get; set; }
        }

        public class Contact
        {
            [Key, Column(Order = 1)]
            public int ID { get; set; }

            public virtual string Nom { get; set; }
        }

        [Table("ContactUsers")]
        public class ContactUser : Contact
        {
            [Key, Column(Order = 2)]
            public int User_ID { get; set; }
            [ForeignKey("User_ID")]
            public virtual User User { get; set; }
        }
ContactUser have the following composite key { ID, User_ID } and User_ID is a non nullable foreign key targeting User. I'm expecting of EF to state the ContactUser as Deleted when I remove the relationship between ContactUser and his User. Which work fine when I don't map Contact to the model.

See :
        public class Context : DbContext
        {
            public Context(string connectionString)
                : base(connectionString)
            {

            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>();
                modelBuilder.Entity<ContactUser>();
                base.OnModelCreating(modelBuilder);
            }
        }

        static void Main(string[] args)
        {
            try
            {
                using (var context = new Context("TestAdhoc"))
                {
                    User User = new User() { Nom = "Hello" };
                    ContactUser contact = new ContactUser() { Nom = "World" };
                    User.Contacts.Add(contact);
                    context.Set<User>().Add(User);
                    context.SaveChanges();
                }

                using (var context = new Context("TestAdhoc"))
                {
                    User User = context.Set<User>().Single(c => c.Nom == "Hello");
                    ContactUser Contact = User.Contacts.Single(c => c.Nom == "World");
                    Console.WriteLine(string.Format("Contact Loaded : {0}", context.Entry<ContactUser>(Contact).State));
                    User.Contacts.Remove(Contact);
                    Console.WriteLine(string.Format("Relationship Removed : {0}", context.Entry<ContactUser>(Contact).State));
                    context.SaveChanges();
                }
            }
            catch(Exception e)
            {

            }
        }
Output :
Contact Loaded : Unchanged
Relationship Removed : Deleted
But now, I map contact :
        public class Context : DbContext
        {
            public Context(string connectionString)
                : base(connectionString)
            {

            }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>();
                modelBuilder.Entity<Contact>();
                modelBuilder.Entity<ContactUser>();
                base.OnModelCreating(modelBuilder);
            }
        }
Output :
Loaded : Unchanged
Removed : Modified
As you can see, the behavior is not the same anymore.

Viewing all articles
Browse latest Browse all 1793

Trending Articles



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