0

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? 
Sahil Sharma
  • 1,813
  • 1
  • 16
  • 37

1 Answers1

0

In Address, Payroll, Contact try to remove the foreign keys and in primary keys use something like this [Key, ForeignKey("Employee")].

UPDATE: For example Address Class should be:

Table("tbl_Address")]
public class Address: BaseEntity
{
    [Key, ForeignKey("Employee")]
    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; }

    public virtual Employee Employee { get; set; }
}
Kostis
  • 953
  • 9
  • 21
  • I tried it and it worked for one Entity. I want EmployeeID to be the foreign key of all the three entites and have tried this: [Key, ForeignKey("Address, Contact, Payroll")] but not working. I also tried three ForeignKey attributes but it is not allowed :( – Sahil Sharma Dec 11 '16 at 12:26
  • Yes I tried the same thing but the issue arises in case of Employee Scenario. The EmployeeId is Foregin key for three tables - Address, Contact, Payroll. So what I'm trying to achieve is the attribute over EmployeeId as [Key, ForeignKey("Address","Contact","Payroll")] which is not permissible. – Sahil Sharma Dec 12 '16 at 03:42
  • In Employee class you only assign the key annotation to EmployeeId – Kostis Dec 12 '16 at 08:46
  • What type of relationship do you want? In the title you mention 1 to many but in your description you say 1:1 – Kostis Dec 12 '16 at 09:08
  • Then you don't need EmployeeId to be foreign key to other tables. ;) – Kostis Dec 12 '16 at 10:34