[英]Entity Framework 6.1: Code First navigation properties that do not have corresponding keys defined in database
I am trying the Code First approach with EF 6.1 (I'm very new to it) and I have a weird issue with a navigation property based on a 1.1 or 1.0 relationship in the Database. 我正在尝试使用EF 6.1的“代码优先”方法(这是我的新手),但是基于数据库中1.1或1.0关系的导航属性存在一个奇怪的问题。
I have a User class mapped to a User table that looks like: 我有一个映射到User表的User类,如下所示:
[Table("TL_MF_SEC_USER_MST")]
public class User
{
/// <summary>
/// Table Column : USER_ID
/// </summary>
[Key, Column("USER_ID")]
public decimal ID { get; set; }
/// <summary>
/// Table Column : USER_CODE
/// </summary>
[Column("USER_CODE")]
public string Code { get; set; }
/// <summary>
/// Table Column : DOMAIN_NM
/// </summary>
[Column("DOMAIN_NM")]
public string Domain { get; set; }
/// <summary>
/// Table Column : EMPLOYEE_ID
/// </summary>
[Column("EMPLOYEE_ID")]
public string EmployeeID { get; set; }
And an Employee class mapped to an Employee table that looks like: 并将Employee类映射到Employee表,如下所示:
[Table("TL_MF_EMPLOYEE_MST")]
public class Employee
{
/// <summary>
/// Table Column : EMPLOYEE_CODE
/// </summary>
[Key, Column("EMPLOYEE_CODE")]
public string EmployeeID { get; set; }
/// <summary>
/// Table Column : EMPLOYEE_NAME
/// </summary>
[Column("EMPLOYEE_NAME")]
public string EMPLOYEE_NAME { get; set; }
public string Manager { get; set; }
The problem is that the "Keys" between these tables are the EmployeeID property (not a primary key) from the User class which maps to the EmployeeID property (which is also not a primary or foreign key in the DB) in the Employee class. 问题在于这些表之间的“键”是User类中的EmployeeID属性(不是主键),它映射到Employee类中的EmployeeID属性(在数据库中也不是主键或外键)。
Thus, I am trying to create a navigation property on User to Employee but I cannot figure out how to tell EF to use these two non-key fields as the properties to match the user on. 因此,我试图在“用户到雇员”上创建导航属性,但无法弄清楚如何告诉EF将这两个非关键字段用作与用户匹配的属性。
Unfortunately, I am in an environment where I probably can't make significant changes to the actual database schema as this structure is used for administering permissions in at least 8 apps (some of which have 20 plus instances). 不幸的是,我处于无法对实际数据库架构进行重大更改的环境中,因为该结构用于管理至少8个应用程序(其中有20个以上实例)中的权限。 Some small changes (like adding an index or constraint) might work though.
不过,可能会进行一些小的更改(例如添加索引或约束)。
If there is a way to do this via attributes or fluent api, I would really appreciate a heads up. 如果有一种方法可以通过属性或流利的api做到这一点,那么我将非常感谢。 Any recommendations are welcome.
欢迎任何建议。
Thanks in advance. 提前致谢。
EF only accepts associations to primary key properties. EF仅接受与主键属性的关联。 But it only looks at primary keys defined in the model.
但是,它仅查看模型中定义的主键。 So, because of the
Key
attribute on EmployeeID
, you can use it in associations, like so: 因此,由于
EmployeeID
的Key
属性,您可以在关联中使用它,如下所示:
public class User
{
[Key, Column("USER_ID")]
public decimal ID { get; set; }
...
[Column("EMPLOYEE_ID")]
public string EmployeeID { get; set; }
public Employee Employee { get; set; } // You need this property
}
And in the context's override of OnModelCreating
: 并且在上下文中重写
OnModelCreating
:
modelBuilder.Entity<User>()
.HasRequired(u => u.Employee) // Or HasOptional
.WithMany()
.HasForeignKey(u => u.EmployeeId);
Apparently, seeing this Key
attribute on EmployeeID
, you are certain that EmployeeID
is a candidate key to Employee
. 显然,看到了这个
Key
的属性EmployeeID
,您肯定EmployeeID
是候选关键Employee
。 This must be absolutely guaranteed for this to work correctly. 必须对此绝对保证才能正常工作。
It's clear that you use code-first without migrations. 显然,您使用代码优先而不进行迁移。 With migrations, this mapping would of course create a primary key
EmployeeId
in the database. 通过迁移,此映射当然将在数据库中创建主键
EmployeeId
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.