markp3rry
markp3rry

Reputation: 734

mvc code first modelling multiple relationships - simple example

I have two Entities in an MVC app:

public class User
{
    public int ID {get; set;}
    public string Forename {get; set;}
    public string Surname {get;set;}
}

public class SubGroup
{
    public int ID {get;set;}
    public string Name {get;set;}
}

and I want to use Code First to create a relationship between them. The relationship is that each user can be a member of 0 or more subgroups.

Please can someone suggest the best way to do this because I have seen a few examples on SO and I'm confused. I've seen stuff about implementing ICollections in a class, other stuff about using the fluent API.

Upvotes: 0

Views: 667

Answers (2)

testCoder
testCoder

Reputation: 7385

ICollections approach: UPDATE:

If I understand correctly you have many-to-many relationship (group have many users, user have can be in many groups), in that case code should be like this:

        public class RelationExampleContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<SubGroup> SubGroups { get; set; }

}


public class User
{
    [Key]
    public int ID { get; set; }
    public string Forename { get; set; }
    public string Surname { get; set; }

    public ICollection<SubGroup> SubGroups { get; set; } 
}

public class SubGroup
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public ICollection<User> Users { get; set; }
}

Adding Users to groups example:

 var db = new RelationExampleContext();

var subGroups = new List<SubGroup>
                                {
                                    new SubGroup() {Name = "Subgroup1"},
                                    new SubGroup() {Name = "Subgroup2"}
                                };

            var users = new List<User>
                            {
                                new User()
                                    {
                                        Forename = "Forename1",
                                        Surname = "Surname1",
                                        SubGroups = new List<SubGroup>
                                                        {
                                                            subGroups.First(),
                                                            subGroups.Last()
                                                        }
                                    },
                                new User()
                                    {
                                        Forename = "Forename2",
                                        Surname = "Surname2",
                                        SubGroups = new List<SubGroup>
                                                        {
                                                            subGroups.First()
                                                        }
                                    }
                            };

            foreach (var subGroup in subGroups)
            {
                db.SubGroups.Add(subGroup);
            }

            foreach (var user in users)
            {
                db.Users.Add(user);
            }

            db.SaveChanges();

Upvotes: 1

san4o
san4o

Reputation: 196

I guess User and SubGroup has the same Tables in database you need create relation table between this tables (fields User_id, Group_id) model for new entity like UserGroups

and then you can create composition in User class

Upvotes: 1

Related Questions