EF6 FluentAPI,0:1 单向
EF6 FluentAPI, 0:1 Unidirectional
我花了最后三个小时试图解决这个问题,最后放弃了(我会解决它)。但是...只是为了确定...是否没有办法在 EF6 Fluent API 中设置单向 0:1/1:1?
考虑:
CREATE TABLE LegacyUsers (
ID INT NOT NULL PRIMARY KEY,
UserName NVARCHAR(50),
EmployeeID INT NULL
)
CREATE TABLE Employees (
ID INT NOT NULL PRIMARY KEY,
EmployeeName NVARCHAR(50)
)
领域模型:
public class LegacyUser {
public int ID {get;set;}
public int? EmployeeID {get;set;}
public virtual Employee Employee {get;set;}
}
public class Employee {
public int ID {get;set;}
public string EmployeeName {get;set;}
}
"Fluent"(哈哈)API理论设置:
modelBuilder.Entity<LegacyUser>()
.HasOptional(x => x.Employee)
.WithForgeignKey(x => x.EmployeeID)
我已经研究了几个小时,并尝试了多种方法来配置它。由于我的努力,我得到了 "column name already exists" 验证错误或 "invalid column: Employee_ID" 错误的回报(如果这是双向的,我可以很容易地修复这些错误,但这是一个或多或少的锁定模式)。我发现唯一会迫使它工作的是将它作为 1:M 关系来尝试,它通过必须使用域模型 属性 作为抛出整个 "fluency"一个集合而不是一个简单的、单一的 属性.
真的没有办法像我认为的那样容易吗?要求非常简单:根据旧用户文件中的员工 ID 获取关联的员工对象(无需破坏模型或向数据库添加新字段)
(供参考):
One to zero-or-one with HasForeignKey
Unidirectional One-To-One relationship in Entity Framework
The only thing that I've found that would force it to work is to try it as a 1:M relationship, which throws off the whole "fluency" by having to use the domain model property as a collection rather than a simple, single property.
这确实是建立 Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations 中描述的这种关系的唯一方法。但是你误读了单向部分。幸运的是,"uni" 部分是从属端的单个导航 属性 - 正是外键 属性 所在的位置。
所以你的模型没问题,你只需要这样设置:
modelBuilder.Entity<LegacyUser>()
.HasOptional(e => e.Employee)
.WithMany()
.HasForeignKey(e => e.EmployeeID)
.WillCascadeOnDelete(false);
关键部分是无参数 WithMany
调用。
我花了最后三个小时试图解决这个问题,最后放弃了(我会解决它)。但是...只是为了确定...是否没有办法在 EF6 Fluent API 中设置单向 0:1/1:1?
考虑:
CREATE TABLE LegacyUsers (
ID INT NOT NULL PRIMARY KEY,
UserName NVARCHAR(50),
EmployeeID INT NULL
)
CREATE TABLE Employees (
ID INT NOT NULL PRIMARY KEY,
EmployeeName NVARCHAR(50)
)
领域模型:
public class LegacyUser {
public int ID {get;set;}
public int? EmployeeID {get;set;}
public virtual Employee Employee {get;set;}
}
public class Employee {
public int ID {get;set;}
public string EmployeeName {get;set;}
}
"Fluent"(哈哈)API理论设置:
modelBuilder.Entity<LegacyUser>()
.HasOptional(x => x.Employee)
.WithForgeignKey(x => x.EmployeeID)
我已经研究了几个小时,并尝试了多种方法来配置它。由于我的努力,我得到了 "column name already exists" 验证错误或 "invalid column: Employee_ID" 错误的回报(如果这是双向的,我可以很容易地修复这些错误,但这是一个或多或少的锁定模式)。我发现唯一会迫使它工作的是将它作为 1:M 关系来尝试,它通过必须使用域模型 属性 作为抛出整个 "fluency"一个集合而不是一个简单的、单一的 属性.
真的没有办法像我认为的那样容易吗?要求非常简单:根据旧用户文件中的员工 ID 获取关联的员工对象(无需破坏模型或向数据库添加新字段)
(供参考):
One to zero-or-one with HasForeignKey
Unidirectional One-To-One relationship in Entity Framework
The only thing that I've found that would force it to work is to try it as a 1:M relationship, which throws off the whole "fluency" by having to use the domain model property as a collection rather than a simple, single property.
这确实是建立 Associations in EF Code First: Part 5 – One-to-One Foreign Key Associations 中描述的这种关系的唯一方法。但是你误读了单向部分。幸运的是,"uni" 部分是从属端的单个导航 属性 - 正是外键 属性 所在的位置。
所以你的模型没问题,你只需要这样设置:
modelBuilder.Entity<LegacyUser>()
.HasOptional(e => e.Employee)
.WithMany()
.HasForeignKey(e => e.EmployeeID)
.WillCascadeOnDelete(false);
关键部分是无参数 WithMany
调用。