首页 > 解决方案 > 迁移到 postgres 无法正常工作

问题描述

因此,我构建了一个非常 Asp.Net Core 3.0 MVC 应用程序,其中包含一些模型,当我运行时add-migration initial,它运行时没有错误,并且显示在解决方案资源管理器中,但实际 PostgreSQL 数据库中没有发生任何变化。

我注意到当我弄乱连接字符串并在那里放错东西时,它只在我想删除迁移时才重要,当我想添加迁移时它不会出错。

我正在使用 .net core 3.0 , PostgreSQL 12.1。

ConfigureServicesStartup.cs

services.AddEntityFrameworkNpgsql().AddDbContext<MyDbContext>(opt => opt.UseNpgsql(Configuration.GetConnectionString("DbConnection")));

中的连接字符串appsettings.json

{
  "ConnectionStrings": {
    "DbConnection" :  "User ID =postgres;Password=pass;Server=localhost;Port=5432;Database=MyDb;Integrated Security = True; Pooling=true"
  },

数据库上下文:

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

        public DbSet<PirkVald> PirkValds { get; set; }
    }

运行时的控制台输出add-migration initial

Build started...
Build succeeded.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.1.0 initialized 'MyDbContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
To undo this action, use Remove-Migration.

标签: c#asp.netasp.net-mvcpostgresqlasp.net-core

解决方案


您需要使用Update-database来应用您的迁移并刷新您的 PostgreSQL 数据库


推荐阅读