【问题标题】:EF Core 5.0 Exception Invalid object name [TableName]EF Core 5.0 异常无效的对象名称 [TableName]
【发布时间】:2021-12-03 09:01:52
【问题描述】:

虽然可以确定表存在,但我收到了无效的对象名称错误。它是一个从 4.6 转换到 Core 5 的 VB.NET 应用程序。4.6 版本在数据库中查找表。曾经有一行代码初始化数据库。它是 Me.Database.SetInitializer(Of MyDbContext)(Nothing)

我在 Core 5 中找不到任何等效项。为了解决该错误,我查看了此处的其他问答并尝试了所有方法。还有什么要尝试的吗?

Public Sub New()
        'Original
        'Me.Database.Connection.ConnectionString = sqlConString
        'Me.Database.SetInitializer(Of MyDbContext)(Nothing)

        'New
        Me.Database.EnsureCreated()

    End Sub

    'New
    Protected Overrides Sub OnConfiguring(ByVal ob As DbContextOptionsBuilder)

        If Not ob.IsConfigured Then

            Dim configBuilder As New ConfigurationBuilder()
            configBuilder.SetBasePath(Environment.CurrentDirectory).AddJsonFile("appSettings.json")
            Dim configSection As IConfigurationSection = configBuilder.Build().GetSection("AppSettings")

            ob.UseSqlServer(configSection.GetSection("ConnectionString").Value)

        End If

    End Sub

    Protected Overrides Sub OnModelCreating(ByVal mb As ModelBuilder)

        mb.Entity(Of Store)().ToTable("dbo.Store")
        mb.Entity(Of StoreAttrib)().ToTable("dbo.Store_Attribute")
        mb.Entity(Of GroupStore)().ToTable("dbo.Group_Store")

    End Sub

【问题讨论】:

  • 你首先想做什么?禁用迁移? EF Core 与实体框架非常不同。不要试图让旧代码运行,而是阅读 EF Core 文档以了解如何执行您想要的操作。在运行时,您需要明确地使用apply migrationsDbContext.Database.Migrate()
  • 如果我在项目上运行初始迁移命令,它会显示“内置 IMigrationsCodeGenerator 服务不支持项目语言 VB”。我添加了下面的代码,但结果是一样的: Public Sub New(ByVal options As DbContextOptions(Of SORContext)) MyBase.New(options) MyBase.Database.Migrate() End Sub

标签: vb.net .net-core .net-5 entity-framework-core-5


【解决方案1】:

答案是,EF Core 目前不支持 VB。该应用程序必须转换为 C#。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2020-03-29
    • 2020-07-18
    相关资源
    最近更新 更多