EF 4.1 Inheritance of many-to-many relationships

An embarrassing situation
I have a situation where I have 2 objects, where 1 is inherited from the other, which should be mapped to two separate tables, but the code usage should be around a base of two objects.

More details

public class Team
{
  public virtual int Id { get; set; }
  public virtual ICollection<Employee> Members { get; set; }
}
public class Employee
{
  public virtual int Id { get; set; }
  public virtual string Name { get; set; }
  public virtual ICollection<Team> Teams { get; set; }
}

public class EmployeeInfo : Employee
{
  public virtual int Id { get; set; }
  public virtual decimal Amount { get; set; }
}

We have an existing database schema where Employee and EmployeeInfo are separate tables with FK between EmployeeInfo_Id and Employee_Id.

In our system, "managers" will add Employee to the system with a set of personal information (more properties than indicated above), for example, pay and add them to the team. Other areas of the system will use Team or Employee objects for other purposes. We would like the code to be super simple if you can make a comparison.

, , :

public void Foo(string name, decimal pay)
{
  // create the employee
  var employee = new EmployeeInfo();
  employee.Name = name;
  employee.Pay = pay;

  // add him/her to the team
  _team.Employees.Add(employee);   // the idea being that consumers of the Team entity would not get the separate employee info properties

  // save the context
  _context.SaveChanges();
}

, EmployeeInfo, EmployeeInfo, Employee Employee TeamEmployees.

, "". .

public class TeamConfiguration : EntityTypeConfiguration<Team>
{
    public TeamConfiguration()
    {
        ToTable("Team");

        HasKey(t => t.Id);
        HasMany(t => t.Members).WithMany(m => m.Teams)
            .Map(m =>
                     {
                         m.MapLeftKey("Team_Id");
                         m.MapRightKey("Employee_Id");
                         m.ToTable("TeamEmployees");
                     });
    }
}

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
    public EmployeeConfiguration()
    {
        ToTable("Employee");
        ToTable("EmployeeInfo");

        HasKey(t => t.Id);
        Property(p => p.Name);

        HasMany(m => m.Teams)
            .WithMany(t => t.Members)
            .Map(m =>
                     {
                         m.MapLeftKey("Employee_Id");
                         m.MapRightKey("Team_Id");
                         m.ToTable("TeamEmployees");
                     });
    }
}

, -- , FK Employee_Id EmployeeInfo_Id.

, JR.

+3
1

Discriminator - , , Table Per Hierarchy.
, " (TPT)". EmployeeInfo :

[Table("EmployeeInfo")]
public class EmployeeInfo : Employee 

OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
 ...
 modelBuilder.Entity<EmployeeInfo>().ToTable("EmployeeInfo");
 ...
}  

modelBuilder.Configurations.Add(new EmployeeInfoConfiguration()); OnModelCreating:

public class EmployeeInfoConfiguration : EntityTypeConfiguration<EmployeeInfo>
{
    public EmployeeInfoConfiguration()
    {
        ToTable("EmployeeInfo");
    }
}

EF EmployeeInfo .

, , . , Team:

public Team()
{
    this.Employees = new HashSet<Employee>();
}  

:

public class Team
{
    public Team()
    {
        this.Members = new HashSet<Employee>();
    }
    public virtual int Id { get; set; }
    public virtual ICollection<Employee> Members { get; set; }
}
public class Employee
{
    public Employee()
    {
     this.Teams = new HashSet<Team>();
    }

    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Team> Teams { get; set; }
}

[Table("EmployeeInfo")]
public class EmployeeInfo : Employee
{
    public virtual int Id { get; set; }
    public virtual decimal Amount { get; set; }
}  

DbContext :

public partial class TestEntities : DbContext
{
    public DbSet<Employee> Employees { get; set; }
    public DbSet<EmployeeInfo> Employee_Info { get; set; }
    public DbSet<Team> Teams { get; set; }
}  

Foo:

public static void Foo(string name, decimal pay)
{
    var _team = new Team();
    var context = new TestEntities();
    context.Teams.Add(_team);

    // create the employee
    var employee = new EmployeeInfo();
    employee.Name = name;
    employee.Amount = pay;
    context.Employees.Add(employee);
    context.SaveChanges();

    // add him/her to the team
    _team.Members.Add(employee);   

    // save the context
    context.SaveChanges();
}  

, ToTable("EmployeeInfo"); EmployeeConfiguration, .

Table Per Type .

+3

All Articles