简体   繁体   English

不在实体框架6中工作的实体之间的一对一关系

[英]One to one relationship between entities not working in Entity Framework 6

I have the requirement of store data in a relationship between three tables using Entity Framework as: 我要求使用Entity Framework将数据存储在三个表之间的关系中:

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. 从技术上讲,我正在尝试将EmployeeId作为Foregin键链接到所有三个表-地址,工资和联系方式。 Also, in Employee table, I'm storing the AddressId, PayrollId and ContactId as foreign keys which results in 1:1 relatopnship. 另外,在Employee表中,我将AddressId,PayrollId和ContactId存储为外键,这导致1:1的重新关联。 I'm trying to build the schema as follows: 我正在尝试如下构建架构:
Employee entity - Employee.cs 员工实体-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 地址实体-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 联系人实体-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 薪资实体-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'. 无法确定类型'NewEmployeeBuddy.Data.Entities.Employee.Employee'和'NewEmployeeBuddy.Data.Entities.Employee.Address'之间的关联的主体端。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 必须使用关系流利的API或数据注释显式配置此关联的主要端。

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? 

In Address, Payroll, Contact try to remove the foreign keys and in primary keys use something like this [Key, ForeignKey("Employee")]. 在“地址”,“薪资”,“联系人”中尝试删除外键,而在主键中则使用类似[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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM