Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example Entity Framework One to Many and Many to Many
namespace ComplexEntityFramework
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
public class Model1 : DbContext
{
public Model1()
: base("name=Model1")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Event> Events { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(s => s.EventsOrganized)
.WithRequired(c => c.Organizer)
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasMany(s => s.EventsSubscribedFor)
.WithMany(c => c.Participants)
.Map(cs =>
{
cs.MapLeftKey("UserId");
cs.MapRightKey("EventId");
});
base.OnModelCreating(modelBuilder);
}
}
public class User
{
public User()
{
EventsOrganized = new HashSet<Event>();
EventsSubscribedFor = new HashSet<Event>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Event> EventsOrganized { get; set; }
public virtual ICollection<Event> EventsSubscribedFor { get; set; }
}
public class Event
{
public Event()
{
Participants = new HashSet<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int OrganizerId { get; set; }
public virtual User Organizer { get; set; }
public ICollection<User> Participants { get; set; }
}
}
using System;
using System.Linq;
namespace ComplexEntityFramework
{
class Program
{
static void Main(string[] args)
{
var context = new Model1();
var user1 = new User() { Name = "name1" };
var user2 = new User() { Name = "name2" };
var user3 = new User() { Name = "name3" };
var event1 = new Event { Name = "event1", Organizer = user1 };
var event2 = new Event { Name = "event2", Organizer = user2 };
var event3 = new Event { Name = "event3", Organizer = user3 };
event1.Participants.Add(user2);
event1.Participants.Add(user3);
event2.Participants.Add(user1);
event2.Participants.Add(user3);
event3.Participants.Add(user1);
event3.Participants.Add(user2);
context.Users.Add(user1);
context.Users.Add(user2);
context.Users.Add(user3);
context.Events.Add(event1);
context.Events.Add(event2);
context.Events.Add(event3);
context.SaveChanges();
var events = context.Events.ToList();
var users = context.Users.ToList();
foreach(var e in events)
{
Console.WriteLine($"Event: {e.Name} is organized by {e.Organizer.Name}, and has {e.Participants.Count} participants.");
Console.WriteLine("The Participants are: ");
foreach (var p in e.Participants)
{
Console.WriteLine($" {p.Name}");
}
}
foreach(var u in users)
{
Console.WriteLine($"User {u.Name} has the following information: ");
foreach(var eo in u.EventsOrganized)
{
Console.WriteLine($" Organized {eo.Name}");
}
foreach (var esf in u.EventsSubscribedFor)
{
Console.WriteLine($" Participating in {esf.Name}");
}
}
Console.WriteLine("Press any key to exit.");
var x = Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment