【问题标题】:Dot Net Entity Framework database update doesn't create tables in mysql databaseDot Net Entity Framework 数据库更新不在 mysql 数据库中创建表
【发布时间】:2016-09-22 13:12:03
【问题描述】:

我使用MySql 作为官方连接提供商的数据库。我在 mac 上尝试下一个项目示例(asp.net core 1.0):

public class BloggingContext : DbContext
{
    public BloggingContext(DbContextOptions<BloggingContext> options)
        : base(options)
    { }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; set; }
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

在我的 Startup.cs 中

    public void ConfigureServices(IServiceCollection services)
    {
        var connection = @"server=localhost;userid=efcore;port=3306;database=blogsFinal;sslmode=none;";
        services.AddDbContext<BloggingContext>(options => options.UseMySQL(connection));

        // Add framework services.
        services.AddMvc();
    }

在我的 project.json 中我还添加了

"dependencies": {
   ...
     "MySql.Data.EntityFrameworkCore": "7.0.5-ir21",

    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
}

  "tools": {
    ...,
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

当我运行 dotnet ef migrations add v1 时,工作正常并创建了迁移文件,但是当我执行 dotnet ef database update 时,创建了数据库但没有创建表并抛出此输出

System.NotImplementedException: The method or operation is not implemented.
   at MySQL.Data.EntityFrameworkCore.Migrations.Internal.MySQLHistoryRepository.get_ExistsSql()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.GetAppliedMigrations()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
The method or operation is not implemented.

【问题讨论】:

  • 能否请您发布为所述更新执行的代码,这是抛出错误?
  • 这是代码 link 错误是当我执行 dotnet ef 数据库更新时

标签: c# mysql database entity-framework asp.net-core


【解决方案1】:

例外说明了这一点。 Oracle 的官方 MySQL 提供程序还不支持它的迁移或脚手架。

它只会在第一次执行context.Database.EnsureCreated() 时创建数据库。之后所做的任何更改都不会应用。您必须删除整个数据库并创建一个新数据库,从而丢失所有数据。

感谢甲骨文;)

更新

随着 7.0.6-IR31 package 的发布,迁移确实有效,但脚手架仍然没有。

【讨论】:

  • 谢谢,使用 SapientGuardian.EntityFrameworkCore.MySql 可以正常工作
  • 文档链接会有所帮助。
【解决方案2】:

正如 Tseng 所说,这是一个已知问题。这是 2016 年 9 月 14 日的错误报告。

缺少运行 EntityFramework Core 代码第一次迁移的实现

http://bugs.mysql.com/bug.php?id=82981

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2017-07-10
    • 1970-01-01
    相关资源
    最近更新 更多