I have the requirement of store data in a relationship between three tables using Entity Framework as:
One to Many
Employee --> Address
Employee --> Payroll
Employee --> Contact
This relationship indicates that for every Employee, there is Address, Payroll and Contact details. Technically, I'm trying to link EmployeeId as foregin key to all the three tables - Address, Payroll and Contact. Also, in Employee table, I'm storing the AddressId, PayrollId and ContactId as foreign keys which results in 1:1 relatopnship.
I'm trying to build the schema as follows:
Employee entity - Employee.cs
<code>
[Table("tbl_Employee")]
public class Employee: BaseEntity
{
[Key]
public Guid EmployeeId { get; set; }
[MaxLength]
public string FirstName { get; set; }
[MaxLength]
public string MiddleName { get; set; }
[MaxLength]
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Gender { get; set; }
public bool IsActive { get; set; }
[Required]
public Guid AddressId { get; set; }
[Required]
public Guid ContactId { get; set; }
[Required]
public Guid PayrollId { get; set; }
//FK References
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
[ForeignKey("ContactId")]
public virtual Contact Contact { get; set; }
[ForeignKey("PayrollId")]
public virtual Payroll Payroll { get; set; }
}
</code>
Address Entity - Address.cs
<code>
[Table("tbl_Address")]
public class Address: BaseEntity
{
[Key]
public Guid AddressId { get; set; }
public string House { get; set; }
public string Ward { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string PinCode { get; set; }
public string AreaCode { get; set; }
public string Landmark { get; set; }
[Required]
public Guid EmployeeId { get; set; }
//FK References
[ForeignKey("EmployeeId")]
public virtual Employee Employee { get; set; }
}
</code>
Contact Entity - Contact.cs
<code>
[Table("tbl_Contact")]
public class Contact: BaseEntity
{
[Key]
public Guid ContactId { get; set; }
public string Landline { get; set; }
public string Mobile { get; set; }
public string Fax { get; set; }
public string EmailAddress { get; set; }
[Required]
public Guid EmployeeId { get; set; }
//FK References
[ForeignKey("EmployeeId")]
public virtual Employee Employee { get; set; }
}
</code>
Payroll Entity - Payroll.cs
<code>
[Table("tbl_Payroll")]
public class Payroll: BaseEntity
{
[Key]
public Guid PayrollId { get; set; }
public decimal BasicPay { get; set; }
public decimal FlexiblePay { get; set; }
public decimal PFContribution { get; set; }
public decimal Allowances { get; set; }
public decimal TotalPay { get; set; }
[Required]
public Guid EmployeeId { get; set; }
//FK References
[ForeignKey("EmployeeId")]
public virtual Employee Employee { get; set; }
}
</code>
When I'm trying to use Package Manager Console to Add-Migration, it displays the following error:
Unable to determine the principal end of an association between the types 'NewEmployeeBuddy.Data.Entities.Employee.Employee' and 'NewEmployeeBuddy.Data.Entities.Employee.Address'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I googled about it and found this link http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr howeverstill getting the same error. Can someone please suggest any change in the code?