【问题标题】:Entitiy framework Core in VB.NET - How?VB.NET 中的实体框架核心 - 如何?
【发布时间】:2017-07-30 10:09:04
【问题描述】:

我目前正在学习如何使用来自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
[...]

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 2020-05-10
      • 2020-09-12
      相关资源
      最近更新 更多