SQL Server数据库不是由Entity Framework创建的

时间:2015-03-15 05:48:03

标签: sql-server vb.net entity-framework

我正在尝试使用Visual Studio .Net 2013社区版的Entity Framework 6.1.3。问题是没有创建数据库,DBSet类型的属性总是设置为Nothing。

这是我的班级

Public Class ExpenseType
    Public Property ExpensTypeId As Integer
    Public Property ExpenseName As String
    Public Property IsOutgoing As Boolean
End Class

这是我的上下文类

Imports System.Data.Entity

Public Class ExpensesContext
    Inherits DbContext

    Public Property ExpenseTypes As DbSet(Of ExpenseType)
End Class

这是我的表单代码

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using db As New ExpensesContext
        Dim expensetype As New ExpenseType With
        {
                .ExpensTypeId = 0,
                .ExpenseName = "ADDITIONAL FUND",
                .IsOutgoing = False
        }

        **** value of ExpensesTypes is Nothing and generates NullReferenceException
        db.ExpenseTypes.Add(expensetype)
        db.SaveChanges()
    End Using
End Sub

我的连接字符串(在App.Config中)

<connectionStrings>
    <add name="ExpensesContext" 
         connectionString="Server=localhost;Integrated Security=SSPI;Database=ExpensesTracking" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

请帮忙。

提前致谢。

1 个答案:

答案 0 :(得分:0)

解决。我忘了将PROPERTY放在DBSet变量中。