На этом сайте я нашел статью, в которой рассказывалось, как это реализовать (http://bit.ly/tmU8f3).
По сути, нам нужно создать сущность для таблицы сопоставления также в коде.
Вот пример того же самого:
namespace ConsoleApplication1.EF
{
[Table("Emp", Schema="Common")]
public class Emp
{
public Emp()
{
//Projects = new HashSet<Project>();
ProjectEmps = new HashSet<ProjectEmps>();
}
public virtual Guid EmpId { get; set; }
public virtual string EmpName { get; set; }
//public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<ProjectEmps> ProjectEmps { get; set; }
}
[Table("Project", Schema="Common")]
public class Project
{
public Project()
{
//Emps = new HashSet<Emp>();
ProjectEmps = new HashSet<ProjectEmps>();
}
public virtual Guid ProjectId { get; set; }
public virtual string ProjectName { get; set; }
//public virtual ICollection<Emp> Emps { get; set; }
public virtual ICollection<ProjectEmps> ProjectEmps { get; set; }
}
[Table("ProjectEmps", Schema = "Common")]
public class ProjectEmps
{
public Emp Emp { get; set; }
public Project Project { get; set; }
public Guid Emp_EmpId { get; set; }
public Guid Project_ProjectId { get; set; }
public Guid CustomerId { get; set; }
}
public class EmpContext : DbContext
{
public DbSet<Emp> Emps { get; set; }
public DbSet<Project> Projects { get; set; }
public DbSet<ProjectEmps> ProjectEmps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new EmpConfiguration());
modelBuilder.Configurations.Add(new ProjectConfiguration());
modelBuilder.Entity<ProjectEmps>().HasKey(p => new {p.Project_ProjectId, p.Emp_EmpId})
.HasRequired(p => p.Emp).WithMany(p => p.ProjectEmps).HasForeignKey(p => p.Emp_EmpId);
modelBuilder.Entity<ProjectEmps>().HasRequired(p => p.Project).WithMany(p => p.ProjectEmps).HasForeignKey(p => p.Project_ProjectId);
modelBuilder.Entity<ProjectEmps>().ToTable("ProjectEmps", "Common");
base.OnModelCreating(modelBuilder);
}
}
public class EmpConfiguration : EntityTypeConfiguration<Emp>
{
public EmpConfiguration()
{
ToTable("Common.Emp");
this.HasMany(i => i.ProjectEmps).WithRequired(e => e.Emp);
}
}
public class ProjectConfiguration : EntityTypeConfiguration<Project>
{
public ProjectConfiguration()
{
ToTable("Project","Common");
this.HasMany(i => i.ProjectEmps)
.WithRequired(p => p.Project);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer<EmpContext>(new DropCreateDatabaseAlways<EmpContext>());
var p1 = new Project() { ProjectId = Guid.NewGuid(), ProjectName = "Fun Boat" };
var p2 = new Project() { ProjectId = Guid.NewGuid(), ProjectName = "Jumbo Jet" };
var p3 = new Project() { ProjectId = Guid.NewGuid(), ProjectName = "Free Zoo" };
var e1 = new Emp() { EmpId = Guid.NewGuid(), EmpName = "Wriju" };
var e2 = new Emp() { EmpId = Guid.NewGuid(), EmpName = "Wrishika" };
var e3 = new Emp() { EmpId = Guid.NewGuid(), EmpName = "Saswati" };
var pe1 = new ProjectEmps()
{
CustomerId = Guid.NewGuid(),
Emp = e1,
Project = p1,
Emp_EmpId = e1.EmpId,
Project_ProjectId = p1.ProjectId
};
var pe2 = new ProjectEmps()
{
CustomerId = Guid.NewGuid(),
Emp = e2,
Project = p2,
Emp_EmpId = e2.EmpId,
Project_ProjectId = p2.ProjectId
};
var pe3 = new ProjectEmps()
{
CustomerId = Guid.NewGuid(),
Emp = e3,
Project = p3,
Emp_EmpId = e3.EmpId,
Project_ProjectId = p3.ProjectId
};
p1.ProjectEmps.Add(pe1);
p2.ProjectEmps.Add(pe2);
p3.ProjectEmps.Add(pe3);
using (var ctx = new EmpContext())
{
ctx.Projects.Add(p1);
ctx.Projects.Add(p2);
ctx.Projects.Add(p3);
ctx.SaveChanges();
}
}
}
}