Takže konkrétně to bude:
internal class Employee
{
#region Configuration class
internal sealed class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
#region constructors and destructors
public EmployeeConfiguration()
{
//Primary Key
this.HasKey(t => t.EmployeeID);
//Relationships
//this.HasOptional(o => o.EmployeeInCompany).WithRequired(o => o.Employee); //1:1
}
#endregion
}
#endregion
#region member varible and default property initialization
[Key]
public int EmployeeID { get; set; }
public EmployeeInCompany EmployeeInCompany { get; set; }
#endregion
}
internal class Company
{
#region Configuration class
internal sealed class CompanyConfiguration : EntityTypeConfiguration<Company>
{
#region constructors and destructors
public CompanyConfiguration()
{
//Primary Key
this.HasKey(t => t.CompanyID);
//Relationships
//this.HasMany(o => o.EmployeesInCompany).WithRequired(o => o.Company).HasForeignKey(f => f.CompanyID); //1:N
}
#endregion
}
#endregion
#region member varible and default property initialization
[Key]
public int CompanyID { get; set; }
public ICollection<EmployeeInCompany> EmployeesInCompany { get; set; }
#endregion
}
internal class EmployeeInCompany
{
#region Configuration class
internal sealed class EmployeeInCompanyConfiguration : EntityTypeConfiguration<EmployeeInCompany>
{
#region constructors and destructors
public EmployeeInCompanyConfiguration()
{
//Primary Key
this.HasKey(t => t.EmployeeID);
//Relationships
this.HasRequired(o => o.Company).WithMany(o => o.EmployeesInCompany).HasForeignKey(f => f.CompanyID); //1:N
this.HasRequired(o => o.Employee).WithOptional(p => p.EmployeeInCompany); //1:1
}
#endregion
}
#endregion
#region member varible and default property initialization
[Key]
public int EmployeeID { get; set; }
public int CompanyID { get; set; }
public Company Company { get; set; }
public Employee Employee { get; set; }
#endregion
}
Tabulka EmployeeInCompany je 1:1 k tabulce Employee a 1:N k tab. Company. Ty vazby stačí samozřejmě popsat jen z jedné strany a je jedno z které, ale pro úplnost jsem je tam zaremovaně dal, aby bylo vidět, jak by to bylo. Druhou variantou je nechat pouze dvě tabulky Employee a Company a do tabulky Employee přidat CompanyID buď typu int nebo int? podle toho, zda chcete umožnit zaměstnance, který není rovnou přiřazen do firmy (v první variantě je to na úrovni db nevynucené vždycky).
|