dotnet ef迁移添加无法执行:connectionString为空

时间:2020-01-14 11:21:08

标签: c# asp.net-mvc entity-framework

我正在尝试在程序中创建迁移。但是,当我运行命令时(在命令提示符下):

dotnet ef migrations add Initial -c idDataContext

它返回连接字符串为空,更准确地说:

System.ArgumentNullException:值不能为null。
参数名称:connectionString

在Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty(字符串值,字符串parameterName)
在Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(DbContextOptionsBuilder optionsBuilder,String connectionString,Action`1 sqlServerOptionsAction)
在MVF2.Startup.b__8_0(DbContextOptionsBuilder选项)中的C:\ Users \ joao.dias \ source \ repos \ MVF2 \ MVF2 \ Startup.cs:line 45

我还尝试运行程序包管理器控制台,并尝试在其中执行:

add-migration Initial -c idDataContext

我收到相同的错误,并添加了警告:

EF Core工具版本“ 2.1.1-rtm-30846”比运行时版本“ 2.1.11-servicing-32099”更旧。更新工具以获取最新功能和错误修复

我的appsettings.jsonaddsettings.Development.json

{
  ...
  "ConnectionStrings": {
    "fmDataContext": "Server=MY-SERVER\\SQLEXPRESS;Database=MYDB;User ID=MYSELF\\myself.me;Integrated Security=SSPI;",
    "idDataContext": "Server=(localdb)\\MSSQLLocalDB;Database=IdentityUsers;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
...
}

(我的程序有2个数据库上下文…)

我的Startup.cs:

namespace MVF2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public AppFeatures features { get; set; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<idDbContext>(options =>
            {
                var connectionString = Configuration.GetConnectionString("idDbContext");
                options.UseSqlServer(connectionString);
            });

            services.AddIdentity<UserModel, IdentityRole>()
                .AddEntityFrameworkStores<idDbContext>()
                .AddDefaultTokenProviders();

            services.AddDbContext<fmDataContext>(options =>
            {
                var connectionString = Configuration.GetConnectionString("fmDataContext");
                options.UseSqlServer(connectionString);
            });

            services.AddTransient<AppFeatures>(x => (features = new AppFeatures
            {
                DevelopmentEnvironment = Configuration.GetValue<bool>("AppFeatures:DevelopmentEnvironment"),
                SqlServerCredentials = Configuration.GetConnectionString("fmDataContext")
            }));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddMemoryCache();

            services.AddSession();
        }

        public void Configure(IApplicationBuilder app, 
                              IHostingEnvironment env, 
                              AppFeatures features)
        {
            app.UseExceptionHandler("/Error.html");

            app.UseAuthentication();

            app.Use(async (context, next) =>
            {
                if (context.Request.Path.Value.Contains("error"))
                    throw new Exception("ERROR!");

                await next();
            });

            app.UseSession();

            app.UseMvc(routes =>
            {
                routes.MapRoute("Default",
                    "{controller=Login}/{action=Login}/{id?}");
            });

            app.UseFileServer();
        }
    }
}

有人可以解决吗?谢谢。

0 个答案:

没有答案