【问题标题】:One to one relationship between entities not working in Entity Framework 6在实体框架 6 中不起作用的实体之间的一对一关系
【发布时间】:2017-04-26 10:15:28
【问题描述】:

我需要使用实体框架将数据存储在三个表之间的关系中:

一对多
员工 --> 地址
员工 --> 工资单
员工 --> 联系方式

这种关系表明每个员工都有地址、工资单和联系方式。从技术上讲,我正在尝试将 EmployeeId 作为外键链接到所有三个表 - 地址、工资单和联系人。此外,在 Employee 表中,我将 AddressId、PayrollId 和 ContactId 存储为外键,从而产生 1:1 的关系。 我正在尝试按如下方式构建架构:
员工实体 - 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.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.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.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>

当我尝试使用包管理器控制台添加迁移时,它显示以下错误:

无法确定之间关联的主体端 类型“NewEmployeeBuddy.Data.Entities.Employee.Employee”和 'NewEmployeeBuddy.Data.Entities.Employee.Address'。主体端 必须使用以下任一方法显式配置此关联 关系流式 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? 

【问题讨论】:

    标签: c# mysql sql sql-server entity-framework


    【解决方案1】:

    在地址、工资单、联系人中尝试删除外键,在主键中使用类似 [Key, ForeignKey("Employee")] 的内容。

    更新: 例如地址类应该是:

    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; }
    }
    

    【讨论】:

    • 我试过了,它适用于一个实体。我希望 EmployeeID 成为所有三个实体的外键并尝试过这个:[Key, ForeignKey("Address, Contact, Payroll")] 但不起作用。我也尝试了三个 ForeignKey 属性,但这是不允许的:(
    • 是的,我尝试了同样的事情,但是在员工场景的情况下会出现问题。 EmployeeId 是三个表的 Foregin 键 - Address、Contact、Payroll。所以我想要实现的是 EmployeeId 的属性为 [Key, ForeignKey("Address","Contact","Payroll")] 这是不允许的。
    • 在 Employee 类中,您只需将键注释分配给 EmployeeId
    • 你想要什么样的关系?在标题中你提到了一对多,但在你的描述中你说的是 1:1
    • 那么你不需要 EmployeeId 是其他表的外键。 ;)
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 2016-07-19
    相关资源
    最近更新 更多