Defining many to many relation in code first entity framework

前端 未结 3 477
孤独总比滥情好
孤独总比滥情好 2021-02-06 13:45

I have 3 classes in my model as you can see below.

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseG         


        
3条回答
  •  执笔经年
    2021-02-06 14:15

    In EntityFramework 6.1, you don't need to do any of this - just add collections of the two types to each class and everything falls into place.

    public class UserProfile {
        public int Id { get; set; }
        public string UserName { get; set; }
        public virtual ICollection MartialArts { get; set; }
    
        public UserProfile() {
            MartialArts = new List();
        }
    }
    
    public class MartialArt {
        public int Id { get; set; }
        public string Name { get; set; }
        // *snip*
        public virtual ICollection UserProfiles { get; set; }
    
        public MartialArt() {
            UserProfiles = new List();
        }
    }
    

提交回复
热议问题