【发布时间】: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