【问题标题】:SQL Server Database not being created by Entity Framework实体框架未创建 SQL Server 数据库
【发布时间】:2015-03-15 05:48:02
【问题描述】:

我正在 Visual Studio .Net 2013 Community Edition 上试用 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>

请帮忙。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      相关资源
      最近更新 更多