我最近一直在使用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
有什么想法导致这个?这必须是命名约定问题或简单的事情,因为在此之前我没有任何其他表模型的错误。
编辑 - 请参阅下面的答案。关于实体框架在这里做了什么非常困惑。
答案 0 :(得分:1)
在Entity Framework中,字段的名称必须与列的名称匹配。
否则EF不会知道如何映射字段,除非您使用列属性修饰属性,就像在答案中一样。
答案 1 :(得分:0)
好的,我想我已经解决了这个问题。我必须对“ppProject”类进行以下更改:
<Column("ProjectManagerID")>
Public Property EmployeeID As Nullable(Of Integer) 'Has to be set, dont know why
如果有人知道为什么我的财产必须被命名为EmployeeID,请告诉我。我很好奇。
答案 2 :(得分:-1)
一个月前我遇到过类似的问题。我放下桌子并创建了一个新的问题,问题解决了。在我尝试了其他不起作用的方法之后