【发布时间】:2023-03-25 22:35:02
【问题描述】:
我正在尝试将部门映射到员工。
这是部门类:
Class Department
Property DepartID As Integer = 0
Property DepartName As String
End Class
如果我将 Employee 类定义如下:
Class Employee
Property EmployeeID As Integer
Property DepartID As Integer
' Navigational Property
Overridable Property Depart As Department
End Class
如您所见,结构非常简单。基本上,员工表存储部门 ID,我想自动填充部门属性。
如果我在 Employee 中定义了 Foreign ID 字段,我可以使用 Fluent API 轻松映射外键:
.HasRequired(Function(e) e.Depart).WithMany.HasForeignKey(Function(e) e.DepartID).WillCascadeOnDelete(False)
但是,我希望在 Employee 表中不定义 ID 属性 (DepartID) 的情况下实现相同的目标。它只是会更干净一些,因为我不必有多个 ID 字段。有可能吗?
【问题讨论】:
-
如果Employee表中没有DepartID你怎么知道Employee属于哪个部门?
标签: mapping ef-code-first