Error :- Value Cannot be null Parameter Name:ConnectionString in .Net Core for two database connections

Error :- Value Cannot be null Parameter Name:ConnectionString in .Net Core for two database connections

连接一个数据库没有出现ConnectionString错误。它运作良好。但是当我添加两个 databaseContext 类 和两个数据库连接时,就会出现这个错误。这是我第一次为后端添加两个数据库。

Startup.cs

            services.AddDbContext<EmployeeDetailContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));

            services.AddDbContext<DepartmentDetailContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DeptConnection")));

            services.AddCors();

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevConnection": "Server=DILAN-PC\SQL2016;Database=Emp;Trusted_Connection=True;MultipleActiveResultSets=True;",
    "DeptConnection": "Server=DILAN-PC\SQL2016;Database=Dept;Trusted_Connection=True;MultipleActiveResultSets=True;"
  }
}

EmployeeDetailContext.cs

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

        }
            public DbSet<Employee> EmployeeDetails { get; set; }


    }

DepartmentDetailContext.cs

public class DepartmentDetailContext : DbContext
    {

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

        }

        public DbSet<Department> DepartmentDetails { get; set; }

    }

当我尝试添加迁移时,出现此错误。有什么解决办法吗?有两个数据库上下文 类 是否正确?任何人都可以向我推荐任何资源,以便我可以获得更多有关将许多数据库连接到 .net Core 后端的知识吗?

将您的代码更改为:

 services.AddDbContext<EmployeeDetailContext>(options =>
        options.UseSqlServer("Server=DILAN-PC\SQL2016;Database=Emp;Trusted_Connection=True;MultipleActiveResultSets=True;"));

 services.AddDbContext<DepartmentDetailContext>(options =>
        options.UseSqlServer("Server=DILAN-PC\SQL2016;Database=Dept;Trusted_Connection=True;MultipleActiveResultSets=True;"));

然后迁移:

add-migration name -context EmployeeDetailContext
update-database -context EmployeeDetailContext

现在成功了。

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevConnection": "Server=DILAN-PC\SQL2016;Database=Emp;Trusted_Connection=True;MultipleActiveResultSets=True;",
    "DeptConnection": "Server=DILAN-PC\SQL2016;Database=Dept;Trusted_Connection=True;MultipleActiveResultSets=True;"
  }
}

startup.cs


            services.AddDbContext<EmployeeDetailContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));

            services.AddDbContext<DepartmentDetailContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DeptConnection")));

            services.AddCors();

EmployeeDetailContext.cs

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

        }
            public DbSet<Employee> EmployeeDetails { get; set; }


    }

部门详细信息

public class DepartmentDetailContext : DbContext
    {

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

        }

        public DbSet<Department> DepartmentDetails { get; set; }

    }