1

Can any one show me how to do as if done here for implementing member list inside member.

[Table("tb_member")]
public class Member
{
    public int Id { get; set; }
    public string Name { get; set; }

    public List<Member> FriendList { get; set; }
}

for that i have created two tables:

tb_member
id, name

tb_friendlist
id, memberid, friendmemberid (both fk created with tb_member id)
Community
  • 1
  • 1
Abeez Abi
  • 11
  • 2

1 Answers1

1

Seems like you essentially just want to specify the table name for the the M2M relationship. The only way to do this is via fluent configuration:

modelBuilder.Entity<Member>() 
    .HasMany(t => t.FriendList) 
    .WithMany() 
    .Map(m => 
    { 
        m.ToTable("tb_friendlist"); 
        m.MapLeftKey("friendmemberid"); 
        m.MapRightKey("memberid"); 
    });
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • thanks for your reply. we are trying to retrieve friendslist using objDbContext.Entry(mem).Collection(m => m.FriendsList).Load(); and we were getting error:"The property 'FriendsList' on type 'Member' is not a navigation property. The Reference and Collection methods can only be used with navigation properties. Use the Property or ComplexProperty method." – Abeez Abi Aug 27 '14 at 05:59
  • After implementing your suggestion by overriding OnModelCreating, we are getting error when trying to select member itself:"The navigation property 'FriendsList' is not a declared property on type 'Member'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property." – Abeez Abi Aug 27 '14 at 05:59
  • So we just remove the fk relation from database tb_friendlist table. Now we are getting:"One or more validation errors were detected during model generation: MemberMember: Name: The EntitySet 'MemberMember' with schema 'dbo' and table 'friendslist' was already defined. Each EntitySet must refer to a unique schema and table." Can you help us – Abeez Abi Aug 27 '14 at 06:01
  • 1
    thanks for your help Chris, error is fixed now, i was using an unwanted Friendslist class, also one shouldn't remove the FK key relation. – Abeez Abi Aug 27 '14 at 08:46