【问题标题】:Why am i getting argumentNullException when adding migration为什么我在添加迁移时收到 argumentNullException
【发布时间】:2021-09-28 19:04:55
【问题描述】:

我正在尝试在我的 web api 中为一对多数据库创建迁移。服务就是这样配置的,从launchsettings.json成功接收到连接字符串

var connectionStr = Environment.GetEnvironmentVariable("ApplicationDbContext");

services.AddDbContext<InsolationResultContext>(options =>
{
options.UseNpgsql(connectionStr, builder =>
{
        builder.CommandTimeout(300);
        builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
      });
});

这是模型

public class DocumentInsolationResult
    {
        public string Id { get; set; }

        public string UserId { get; set; }

        public IEnumerable<InsolationResult> Elements { get; set; }
    }
public class InsolationResult
    {
        public string UniqueId { get; set; }

        public string Insolation { get; set; }
    }

和 DbContext

public class InsolationResultContext : DbContext
    {
        public DbSet<DocumentInsolationResult> DocumentInsolationResults { get; set; }
        public DbSet<InsolationResult> InsolationResults { get; set; }

        public InsolationResultContext(DbContextOptions<InsolationResultContext> options)
            : base(options)
        {

        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DocumentInsolationResult>()
                .HasMany(p => p.Elements)
                .WithOne();
        }
    }

连接字符串如下所示:

"environmentVariables": {
        "ApplicationDbContext": "Host=192.168.1.***;Port=****;Database=***.******;Username=*****;Password=****",
        "ASPNETCORE_ENVIRONMENT": "Development"
      },

当尝试“添加迁移初始化”时,我总是得到“值不能为空。 (参数'connectionString')。我究竟做错了什么?网上实在找不到答案

upd:我正在从launchsetting接收连接字符串,没关系,因为我在其他一些项目上使用相同的方式获取连接字符串

upd2 硬编码连接字符串对我有用

【问题讨论】:

    标签: c# entity-framework-core database-migration


    【解决方案1】:

    应该是

    "ConnectionStrings": {
        "ApplicationDbContext": "Host=192.168.1.***;Port=****;Database=***.******;Username=*****;Password=****""
      }
    

    在 appsettings.Development.json 文件中。

    因此它给出错误“值不能为空。(参数'connectionString')。”

    在你的 startup.cs 文件中

    services.AddDbContext<InsolationResultContext >(options =>
                {
                    options.UseSqlServer(Configuration.GetConnectionString("ApplicationDbContext")
                });
    

    【讨论】:

    • 感谢您的回复,但它不起作用。连接字符串从launchsettings成功接收,因此将其移至appsettings并更改获取方式不起作用。还是有同样的问题
    【解决方案2】:

    如果您使用 IConfiguration 而不是 Environment.GetEnvironmentVariable,那么它会变得容易得多:

    var connString = config.GetValue<string>("ApplicationDbContext");
    

    【讨论】:

      【解决方案3】:

      问题出在连接字符串上。使用来自 appsettings 或 launchsettings 的连接字符串时,我无法创建迁移。在 strup.cs 中对连接字符串进行硬编码后,我能够创建迁移。现在在数据库初始化后,我正在使用启动设置中的连接字符串。这可能是一些 EF 核心棘手的事情。

      【讨论】:

        猜你喜欢
        • 2020-07-08
        • 2017-06-14
        • 1970-01-01
        • 2021-06-09
        • 2017-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-13
        相关资源
        最近更新 更多