我最近一直在使用 MVC 4 和 Entity Framework 创建一个 Web 应用程序。我的数据库“ppProject”模型进展顺利,如下所示:
Public Class ppProject
<Key()>
Public Property ProjectID As Integer
Public Property ClientID As Integer
Public Property ProjectTitle As String
Public Overridable Property Client As ppClient
Public Overridable Property Milestones As ICollection(Of ppMilestone)
Public Overridable Property Tasks As ICollection(Of ppTask)
End Class
问题是我正在添加一个新的员工表“ppEmployees”。这样,项目就可以有一个 ProjectManager,它是Employees 表的外键。这些是 ProjectManagerID(外键)链接到 EmployeeID(主键)的新模型:
Public Class ppProject
<Key()>
Public Property ProjectID As Integer
Public Property ClientID As Integer
Public Property ProjectTitle As String
Public Property ProjectManagerID As Integer 'NEW'
Public Overridable Property Client As ppClient
Public Overridable Property Milestones As ICollection(Of ppMilestone)
Public Overridable Property Tasks As ICollection(Of ppTask)
Public Overridable Property ProjectManager As ppEmployee 'NEW'
End Class
Public Class ppEmployee
<Key()>
Public Property EmployeeID As Integer
Public Property DepartmentID As Integer
Public Property FirstName As String
Public Property LastName As String
Public Overridable Property ProjectsInManagement As ICollection(Of ppProject)
Public Overridable Property TimeItems As ICollection(Of ppTimeItem)
Public Overridable Property Department As ppDepartment
End Class
当我更改项目模型并添加员工模型时,出现错误
列名“ProjectManager_EmployeeID”无效
触发此操作的代码行是我第一次在视图中访问我的项目时:
@For Each proj In client.Projects
什么是造成这种情况的任何想法?这一定是命名约定问题或一些简单的问题,因为在此之前我的任何其他表模型都没有任何错误。
编辑 -请参阅下面的答案。对实体框架在这里做什么感到非常困惑。