Mohamed Hussein
Mohamed Hussein

Reputation: 13

Many to many relation MVC ASP.NET

I want to make a many to many relationship in a code first Database using Entity Framework ASP.Net and it will be from the same table.

I have users and I want to make every user has his own friends (users too) but i can't establish such a relation in the code first database.

Here is my class.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;

namespace LMS.Model.Entites

{
    public class user
    {
        [Key]
        public int ID { get; set; }
        [Required(ErrorMessage ="Please Insert your name")]
        public string Name { get; set; }
        [Required(ErrorMessage ="Please insert a valid username")]
        public string User_Name { get; set; }
        public string Adress { get; set; }
        [DataType(DataType.Date)] 
        public string Birthday { get; set; }
        [Required]
        [EmailAddress]
        public string E_Mail { get; set; }
        [Required(ErrorMessage = "Please insert a valid password")]
        [MinLength(9)]    
        [DataType(DataType.Password)]
        public string Password { get; set; }
        public string Authority { get; set; }
        public virtual ICollection<user_Friends> friends { get; set; }
    }
} 

I tried to make a new class as a new table to make that many to many relation but it failed.

using LMS.Model.Entites;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LMS.Model
{
    public class user_Friends
    {
        [Key]
        public virtual user USER1 { get; set; }
        [ForeignKey("USER1")]
        public virtual int USER1_ID { get; set; }
        [Key]
        public user USER2 { get; set; }
        [ForeignKey("USER2")]
        public int USER2_ID { get; set; }
    }
}

Upvotes: 1

Views: 61

Answers (1)

Slava Utesinov
Slava Utesinov

Reputation: 13488

At user_Friends class you have two references to user class, where there is only one reference to former. Add one more reference, to complete endpoints connection between classes(at this case you should union these two properties to get full set of friends):

public class user
{
    //other properties  
    [InverseProperty("USER1")]
    public virtual ICollection<user_Friends> friendsSide1 { get; set; } 
    [InverseProperty("USER2")]
    public virtual ICollection<user_Friends> friendsSide2 { get; set; }
}

public class user_Friends
{       
    [Key]
    [Column(Order = 0)]
    [ForeignKey("USER1")]
    public virtual int USER1_ID { get; set; }
    public virtual user USER1 { get; set; }

    [Key]
    [Column(Order = 1)]
    [ForeignKey("USER2")]
    public int USER2_ID { get; set; }
    public virtual user USER2 { get; set; }
}

Also you can simplify your solution this way(without user_Friends class at all, but corresponding table will be created by EF implicitly):

public class user
{
    //other properties      
    public virtual ICollection<user> friends { get; set; }
}

Upvotes: 1

Related Questions