0

I want to implement one-to-many relationship with EF6. Table User can handle many Friends, i can implement that with map table:

--TABLE USERS:
Id

--TABLE USER_MAP:
UserOwnerId
UserFriendId

But how to implement that with EF6?

Here is my entity User:

public class User
{
    ...
    public virtual List<User> Friends { get; set; }
    ...
}
Nickolay Kabash
  • 239
  • 2
  • 12

3 Answers3

1

You can use something like this

// Relationships

HasRequired(t => t.User)
            .WithMany(t => t.Friends)
            .HasForeignKey(d => d.UserId);

https://msdn.microsoft.com/en-us/data/hh134698.aspx

Samrat Alamgir
  • 355
  • 2
  • 13
1

One-to-Many relationship using DataAnnotations:

  public class User
    {
        public User() { }

        public int UserId { get; set; }
        public string Name { get; set; }

        public virtual Friends friends { get; set; }
    }

    public class Friends
    {
        public Friends()
        {
            Users = new List<User>();
        }
        public int FriendId { get; set; }
        public string Name { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }
Badruzzaman
  • 41
  • 1
  • 7
0

you can defined in Code first like that:

1) Fluent API:

public class Student
{
    public Student() { }

    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual Standard Standard { get; set; }
}

public class Standard
{
    public Standard()
    {
        Students = new List<Student>();
    }
    public int StandardId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Student> Students { get; set; }
}

Fleut Api: in your DbContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        //one-to-many 
        modelBuilder.Entity<Student>()
                    .HasRequired<Standard>(s => s.Standard) 
                    .WithMany(s => s.Students);

}

virtual keyword is only for Lazy loading you can remove it if you do not need it

2) Code first:

public class Student
{
    public Student()
    {
        Students= new List<Student>();
    }

    public int StundendId{ get; set; }

    public string StudentName { get; set; }

    public int? SharedStudentId{ get; set; }

    [ForeignKey("SharedStudentId")]
    public Student SharedStudent{ get; set; }

    public virtual ICollection<Student> SharedStudents{ get; set; }
}
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • can't add anything to database, `users` requers `friends`, and `friends` requers `users` to exist in database – Nickolay Kabash May 24 '16 at 13:11
  • this is many to many releation ship and not one to many: I think you need many to many reletionship? – Bassam Alugili May 24 '16 at 13:19
  • this is a good example for many to many http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx ; but the example is with Code First, I think you have database first? – Bassam Alugili May 24 '16 at 13:22