2017-07-30 33 views
0

我目前正在學習如何使用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 
[...] 

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

回答

0

該問題似乎是由於創建的.cs文件。將三個文件轉換爲.vb文件時,將創建所需的表並在EFMigrationsHistory表中創建一個條目。我已經手動轉換將以下文件從CS到VB:

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

和CS文件從項目中取出並添加了vb。如果某人現在知道如何爲vb運行add-migration命令,那將會非常有幫助。