none
Lazy Loading With Fluent API & Relation Table Entities RRS feed

  • Question

  • Hi all,

    I would like to use lady loading but i have some restriction for regular using.

    It's not mandatory but i choosed Fluent API method for defining some rules on my entities but i can not use many to many relation code in this section because i have to define separated relation table entities. The reason is that i have additionnal columns on my relational tables in my database like AddingDatetime, AddingUser, Status etc..

    Thanks for help

    My codes are below. I comment in the codes related to the lazy loading.

    User Entity 

        public class User : BaseEntity<int>
        {
            public User()
            {
                //AllowedUserGroups = new List<UserGroup>();
            }
            public string UserName { get; set; }
    
            [IgnoreDataMember]
            public string Password { get; set; }
    
            [EmailAddress]
            public string Email { get; set; }
    
            //[IgnoreDataMember]
            //public virtual ICollection<UserGroup> AllowedUserGroups { get; set; }
        }

    UserGroup Entity:

        public class UserGroup : BaseEntity<int>
        {
            public UserGroup()
            {
                //AssignedUsers = new List<User>();
            }
    
            public string GroupName { get; set; }
    
            //[IgnoreDataMember]
            //public virtual ICollection<User> AssignedUsers { get; set; }
        }

    Relation table entity for user and usergroup:

        public class UserGroupUser : BaseEntity<int>
        {
            public UserGroupUser()
            {
    
            }
            public int UserGroupId { get; set; }
    
            public int UserId { get; set; }
        }


    Fluent Api code for user:

        public class UserMap : BaseEntityMap<User, int>
        {
            public UserMap()
            {
                ToTable(CustomConvert.GetPsqlTableName<User>());
    
                Property(u => u.Id).HasColumnName(CustomConvert.GetPsqlColumnName<User>(p => p.Id));
                Property(u => u.UserName).IsRequired().HasMaxLength(20).HasColumnName(CustomConvert.GetPsqlColumnName<User>(p => p.UserName));
                Property(u => u.Password).IsRequired().HasMaxLength(50).HasColumnName(CustomConvert.GetPsqlColumnName<User>(p => p.Password));
                Property(u => u.Email).IsRequired().HasMaxLength(50).HasColumnName(CustomConvert.GetPsqlColumnName<User>(p => p.Email));
    
                //HasMany(u => u.AllowedUserGroups)
                //    .WithMany(ug => ug.AssignedUsers)
                //    .Map(m =>
                //            {
                //                m.MapLeftKey(CustomConvert.GetPsqlColumnName<User>(p => p.Id));
                //                m.MapRightKey(CustomConvert.GetPsqlColumnName<UserGroup>(p => p.Id));
                //                m.ToTable(CustomConvert.GetPsqlJoinTable<UserGroup, User>());
                //            });
            }
        }

    Fluent Api for UserGroup

        public class UserGroupMap : BaseEntityMap<UserGroup, int>
        {
            public UserGroupMap()
            {
                ToTable(CustomConvert.GetPsqlTableName<UserGroup>());
                Property(u => u.Id).HasColumnName(CustomConvert.GetPsqlColumnName<UserGroup>(p => p.Id));
                Property(u => u.GroupName).IsRequired().HasMaxLength(25).HasColumnName(CustomConvert.GetPsqlColumnName<UserGroup>(p => p.GroupName));
    
                //HasMany(ug => ug.AssignedUsers)
                //    .WithMany(u => u.AllowedUserGroups)
                //    .Map(m =>
                //    {
                //        m.MapLeftKey("UserId");
                //        m.MapRightKey("UserGroupId");
                //        m.ToTable("UserUserGroup");
                //    });
            }
        }


    • Moved by CoolDadTx Monday, May 1, 2017 2:50 PM EF related
    Saturday, April 8, 2017 9:16 AM

All replies