locked
How to write the modelConfiguration for nested Class? RRS feed

  • Question

  • Three class:

    RootEntity, Group, User.

    RootEntity is an abstract class. Group and User are derived from this class.

    RelationShips: Group can contains many Groups, Group can contain many users, Users may belongs to many Groups.

    I want to put many to many relationship to one table (EntityRelation).

    I need to create the database relationships like the picture I uploaded.

    I want to know the EntityConfiguration code for these three classes.

    I'm using EF6.1 Code

     public abstract class RootEntity
        {
            public Guid ID { get; set; }
            public string Alias { get; set; }
        }

    Group Class

     public class Group : RootEntity
        {
            public string Name { get; set; }
            public virtual ICollection<RootEntity> Entities { get; set; }
        }

    User Class

    public class User : RootEntity
        {
            public string Name { get; set; }
            public string Password { get; set; }
            public virtual ICollection<RootEntity> Entities { get; set; }
        }

    Nested Group User


    : )Ex

    Tuesday, May 6, 2014 2:39 PM

Answers

  • Hello,

    >>RelationShips: Group can contains many Groups, Group can contain many users, Users may belongs to many Groups.

    For these relationship, you can configure it as:

    public class Sample07_02Context : DbContext
    
        {
    
            public Sample07_02Context()
    
            {
    
            }
    
    
            public DbSet<Group> Groups { get; set; }
    
    
            public DbSet<User> Users { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Group>().HasKey(b => b.ID).Property(b => b.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<User>().HasKey(b => b.ID).Property(b => b.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                //Group can contains many users&&//Users may belongs to many Groups.
    
                modelBuilder.Entity<User>().HasMany(u => u.Groups).WithMany(g => g.Users).Map(u => u.MapLeftKey("UserFKID").MapRightKey("GroupFKID"));
    
    
                //Group can contains many Groups
    
                modelBuilder.Entity<Group>().HasMany(g => g.Groups).WithRequired().HasForeignKey(g => g.GroupFKID);
    
            }
    
        }
    
        public abstract class RootEntity
    
        {
    
            public int ID { get; set; }
    
            public string Alias { get; set; }
    
        }
    
        //Group Class
    
        public class Group : RootEntity
    
        {
    
            public string Name { get; set; }
    
    
    
            //Group can contains many Groups
    
            public int GroupFKID { get; set; }
    
            public virtual ICollection<Group> Groups { get; set; }
    
    
            //Group can contains many users
    
            //public int UserFKID { get; set; }
    
            public virtual ICollection<User> Users { get; set; }
    
    
            //public virtual ICollection<RootEntity> Entities { get; set; }
    
        }
    
        //User Class
    
        public class User : RootEntity
    
        {
    
            public string Name { get; set; }
    
            public string Password { get; set; }
    
    
            //Users may belongs to many Groups.
    
            //public int GroupFKID { get; set; }
    
            public virtual ICollection<Group> Groups { get; set; }
    
            //public virtual ICollection<RootEntity> Entities { get; set; }
    
        }
    

    >>I want to put many to many relationship to one table (EntityRelation).

    Entity Framework will generate the junction table automatically for us with their primary keys as foreign keys.

    If you want to configure the junction table, you need to create the EntityRelation entity class and add two one to many relationships with User and Group. For details, you can check this link:

    http://social.msdn.microsoft.com/Forums/en-US/d1925680-17ef-4ee2-8f96-6d668a8921ba/entitytypeconfiguration-and-many-to-many-relationship-with-additional-field?forum=adodotnetentityframework

    >> I need to create the database relationships like the picture I uploaded.

    From the image, I do not see any relationship existing in them except the inheritance. Are your relationships logic? If they are, Entity Framework does not support it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ex.Shen Thursday, May 8, 2014 8:18 AM
    Wednesday, May 7, 2014 6:24 AM
  • Hello,

    What you want I think it is a nice feature, however, the automatically generated junction table that cannot do that for you, so I would like suggest you creating the EntityRelation entity class to log these relationships by hand.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ex.Shen Friday, May 9, 2014 5:31 AM
    Friday, May 9, 2014 5:29 AM

All replies

  • Hello,

    >>RelationShips: Group can contains many Groups, Group can contain many users, Users may belongs to many Groups.

    For these relationship, you can configure it as:

    public class Sample07_02Context : DbContext
    
        {
    
            public Sample07_02Context()
    
            {
    
            }
    
    
            public DbSet<Group> Groups { get; set; }
    
    
            public DbSet<User> Users { get; set; }
    
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Group>().HasKey(b => b.ID).Property(b => b.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
                modelBuilder.Entity<User>().HasKey(b => b.ID).Property(b => b.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    
    
                //Group can contains many users&&//Users may belongs to many Groups.
    
                modelBuilder.Entity<User>().HasMany(u => u.Groups).WithMany(g => g.Users).Map(u => u.MapLeftKey("UserFKID").MapRightKey("GroupFKID"));
    
    
                //Group can contains many Groups
    
                modelBuilder.Entity<Group>().HasMany(g => g.Groups).WithRequired().HasForeignKey(g => g.GroupFKID);
    
            }
    
        }
    
        public abstract class RootEntity
    
        {
    
            public int ID { get; set; }
    
            public string Alias { get; set; }
    
        }
    
        //Group Class
    
        public class Group : RootEntity
    
        {
    
            public string Name { get; set; }
    
    
    
            //Group can contains many Groups
    
            public int GroupFKID { get; set; }
    
            public virtual ICollection<Group> Groups { get; set; }
    
    
            //Group can contains many users
    
            //public int UserFKID { get; set; }
    
            public virtual ICollection<User> Users { get; set; }
    
    
            //public virtual ICollection<RootEntity> Entities { get; set; }
    
        }
    
        //User Class
    
        public class User : RootEntity
    
        {
    
            public string Name { get; set; }
    
            public string Password { get; set; }
    
    
            //Users may belongs to many Groups.
    
            //public int GroupFKID { get; set; }
    
            public virtual ICollection<Group> Groups { get; set; }
    
            //public virtual ICollection<RootEntity> Entities { get; set; }
    
        }
    

    >>I want to put many to many relationship to one table (EntityRelation).

    Entity Framework will generate the junction table automatically for us with their primary keys as foreign keys.

    If you want to configure the junction table, you need to create the EntityRelation entity class and add two one to many relationships with User and Group. For details, you can check this link:

    http://social.msdn.microsoft.com/Forums/en-US/d1925680-17ef-4ee2-8f96-6d668a8921ba/entitytypeconfiguration-and-many-to-many-relationship-with-additional-field?forum=adodotnetentityframework

    >> I need to create the database relationships like the picture I uploaded.

    From the image, I do not see any relationship existing in them except the inheritance. Are your relationships logic? If they are, Entity Framework does not support it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ex.Shen Thursday, May 8, 2014 8:18 AM
    Wednesday, May 7, 2014 6:24 AM
  • Thank you Fred,

              For the table "EntityRelation", Actually I want to keep all many to many relations to that table.

              Group and User are all RootEntities. For example, Group1 belongs to Group2 and Group3, Group1 includes User1 and Group4, User1 belongs to Group2.

              So that in the EntityRelation table there should be 5 rows

              1.Father:Group1FKID,Son:User1FKID

              2.Father:Group1FKID,Son:Group4FKID

              3.Father:Group2FKID, Son:Group1FKID

              4.Father:Group3FKID, Son:Group1FKID

              5.Father:Group2: Son:User1FKID

    From this table, I can know how many entities belongs to Group1

    how many sub entities be included in Group1.

             


    : )Ex

    Thursday, May 8, 2014 9:16 AM
  • Hello,

    What you want I think it is a nice feature, however, the automatically generated junction table that cannot do that for you, so I would like suggest you creating the EntityRelation entity class to log these relationships by hand.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Ex.Shen Friday, May 9, 2014 5:31 AM
    Friday, May 9, 2014 5:29 AM