【问题标题】:Entity Framework Core migrations empty on creationEntity Framework Core 迁移在创建时为空
【发布时间】:2020-06-06 02:05:08
【问题描述】:

我正在尝试在实际的 SQL Server 上创建我的数据库实例,而不仅仅是在本地托管,但每次我尝试创建迁移时,它们都会返回空...

public partial class InitialMigration0 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }

这是我的 appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=hsciytusql2\\trialsdev; Database=Z_MyLymphieDiary;Trusted_Connection=True;MultipleActiveResultSets=true;"
    // "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MLD-1ED06986-5F07-4A1C-85B9-D9F3F477BFF5;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

还有我的 AppDbContext:

namespace MLD.Models
{
    public class AppDbContext : IdentityDbContext<IdentityUser>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {

        }

        //public DbSet<User> Users { get; set; }
        public DbSet<LymphSite> LymphSites { get; set; }
        public DbSet<Measurement> Measurements { get; set; }
        public DbSet<Circumference> Circumferences { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
}

任何想法为什么这会空着回来?它适用于我的 localDb,并且已连接到 SQL Server,但迁移中没有任何内容,它无法创建表,所以我有点茫然!

提前致谢

【问题讨论】:

  • 再次尝试所有步骤,但这次完全删除迁移文件夹。另外我认为您的数据库连接字符串可能不正确。由于这不是本地服务器,因此设置 trusted connection = true 可能无济于事。而是在连接字符串中设置实际的用户 ID 和密码。

标签: entity-framework entity-framework-core migration database-migration entity-framework-migrations


【解决方案1】:

连接字符串解决方案:

我需要为我的连接字符串添加集成安全性,在这种情况下不需要用户 ID 和密码 - 当使用用户 ID 和密码时,用户创建的用户没有创建数据库的权限

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=hsciytusql2\\trialsdev; Database=Z_MyLymphieDiary;MultipleActiveResultSets=true; Integrated Security=true;"
  },

修复迁移问题: x

不知道为什么迁移首先会出错,但它们现在可以工作了!

【讨论】:

    猜你喜欢
    • 2019-02-12
    • 1970-01-01
    • 2021-01-21
    • 2015-05-20
    • 2021-07-26
    • 2021-11-28
    • 2017-10-27
    • 2017-04-26
    • 2016-04-12
    相关资源
    最近更新 更多