VB.NET中的Entitiy框架核心 - 如何?

时间:2017-07-30 10:09:04

标签: .net vb.net entity-framework sqlite code-first

我目前正在学习如何使用https://docs.microsoft.com/en-us/ef/core/get-started/uwp/getting-started中的Sqlite在UWP中创建CodeFirst数据库(4分钟阅读)。我已经按照步骤进行了操作,在C#中一切正常。但是,在将指南翻译成VB.NET时,我得到一个错误,指出我的数据库中没有Blogs表,只要我尝试向其添加数据(单击添加按钮)。

  

SqliteException:SQLite错误1:'没有这样的表:博客'。

在VB.NET中,Add-Migration MyFirstMigration创建一个名为“20170729091234_MyFirstMigration.Designer.cs”的附加(第三个)C#文件。

如何让这个微软示例在VB.NET中运行?

我的模特:

Public Class BloggingContext
Inherits DbContext
    Property Blogs As DbSet(Of Blog)
    Property Posts As DbSet(Of Post)

    Protected Overrides Sub OnConfiguring(optionsBuilder As DbContextOptionsBuilder)
        optionsBuilder.UseSqlite(String.Format("Data Source={0}", "Blogging.db"))
    End Sub
End Class

Public Class Blog
    Property BlogId As Integer
    Property Url As String
    Property Posts As List(Of Post)
End Class

Public Class Post
    Property PostId As Integer
    Property Title As String
    Property Content As String
    Property BlogId As Integer
    Property Blog As Blog
End Class

MainPage.xaml.vb:

Private Sub Add_Click(sender As Object, e As RoutedEventArgs)
    Using _Context As New BloggingContext
        Dim Blog As New Blog With {.Url = NewBlogUrl.Text}
        _Context.Blogs.Add(Blog)
        _Context.SaveChanges() '!!This is where the error occurs!!

        Blogs.ItemsSource = _Context.Blogs.ToList
    End Using
End Sub

Private Sub Page_Loaded(sender As Object, e As RoutedEventArgs)
    Using _Context As New BloggingContext
        Blogs.ItemsSource = _Context.Blogs.ToList
    End Using
End Sub

App.xaml.vb:

[...]
Sub New()
    InitializeComponent()

    Using _Context As New BloggingContext
        _Context.Database.Migrate
    End Using
End Sub
[...]

如果您需要更多信息,我很乐意提供。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题似乎是由于创建的.cs文件。将三个文件转换为.vb文件时,将创建所需的表,并在EFMigrationsHistory表中创建一个条目。我已将以下文件从cs手动转换为vb:

  1. 20170819131117_Inital.cs
  2. 20170819131117_Inital.Designer.cs
  3. DB_ModelModelSnapshot.cs
  4. 从项目中获取的cs文件和vb添加了。如果有人现在知道如何为vb运行add-migration命令,那将非常有用。