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

问题描述:

我目前正在学习如何使用Sqlite从https://docs.microsoft.com/en-us/ef/core/get-started/uwp/getting-started(阅读4分钟)在UWP中创建CodeFirst数据库。我已经遵循了这些步骤,在C#中一切正常。但是,在将指南翻译成VB.NET时,我收到一个错误消息,说明我的数据库中不存在Blogs表,只要我尝试向其中添加数据(单击添加按钮)。Entitiy框架核心在VB.NET中 - 如何?

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

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

我如何得到这个微软的例子在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 
[...] 

如果您需要了解更多信息,我会很高兴提供它。 在此先感谢!

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

  1. 20170819131117_Inital.cs
  2. 20170819131117_Inital.Designer.cs
  3. DB_ModelModelSnapshot.cs

和CS文件从项目中取出并添加了vb。如果某人现在知道如何为vb运行add-migration命令,那将会非常有帮助。