嗨,我尝试将EF与现有数据库的代码首先使用,但我遇到了错误:
{"值不能为null。参数名称:源"}
使用以下行:
Dim ctx = New MVCTest1
Dim t = ctx.Companies.ToList()
我的Poco课程包括下面:
i有一个标准的conn字符串,称为mvctest1,该字符串指向SQL Server。我认为EF正在使用它,因为我可以在DBContext的连接字符串中看到它。连接字符串是否需要特殊格式。我还注意到没有被调用。我不确定这是否相关。非常新鲜,所以我怀疑这是明显的欢呼,克里斯
Imports System.Data.Entity
Public Class Company
Public CompanyID As Integer
Public CompanyName As String
Public Overridable Property Contacts As ICollection(Of Contact)
End Class
Public Class Contact
Public ContactID As Integer
Public FirstName As String
Public LastName As String
Public Overridable Property Company As Company
End Class
Public Class MVCTest1
Inherits DbContext
Public Contacts As DbSet(Of Contact)
Public Companies As DbSet(Of Company)
Protected Overrides Sub OnModelCreating(ByVal modelBuilder As System.Data.Entity.DbModelBuilder)
---Not being called
End Sub
End Class
问题是MVCTest1
中的Contacts
和Companies
是公共成员变量,而不是属性。因此,当您解决它们时,它们不会初始化。