我目前正在學習如何使用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
[...]
如果您需要了解更多信息,我會很高興提供它。 在此先感謝!