在不注册数据存储的情况下在Startup.cs中添加DbContextOptions我的问题是下面的代码在启动期间没有注册数据存储。这是我在应用程序的响应中得到的特定“错误”声明:处理请求时发生未处理的异常。InvalidOperationException:没有配置数据存储。通过覆盖DbContext类或AddDbContext方法中的OnConfiguring配置数据存储设置services.Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore(ServiceProviderSourceproviderSource)在ConfigureServices(IServiceCollection服务)中,我试图为指定DbContextOptions我在lambda中的DbContext。代码:services.AddEntityFramework(Configuration).AddSqlServer().AddDbContext(options=>options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")));在我的DbContext中,我有一个向base发送选项的构造函数,代码:publicMyContext(DbContextOptionsoptions):base(options){}我的配置文件config.json在启动时读取并包含以下连接字符串:“Data”:{“DefaultConnection”:{“ConnectionString”:“Server=(localdb)\MSSQLLocalDB;Database=MyDbName;Trusted_Connection=True;MultipleActiveResultSets=True;”我使用了protectedoverridevoidOnConfiguring(DbContextOptionsoptions){options.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));}在我的DbContext中成功。它注册了数据存储并且工作正常,但我宁愿使用lambda方式。如果需要更多信息,我会提供。您是将上下文注入控制器还是在您使用它的任何地方?我发现如果您尝试新建上下文而不是注入它,它不会使用Startup.cs中指定的配置我(仍然)在EF7和beta4上遇到同样的问题。这是我的数据上下文中的解决方法:publicclassAspNetDataContext:IdentityDbContext,IDataContext{privatereadonlystring_connectionString;公共DbSet玩家{得到;放;}publicAspNetDataContext(DbContextOptionsoptions){_connectionString=((SqlServerOptionsExtension)options.Extensions.Extensions.First()).ConnectionString;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){optionsBuilder.UseSqlServer(_connectionString);我从选项中提取连接字符串并在OnConfiguring方法中使用它。这仍然不是我们想要的解决方案,但我不需要更改Startup.cs中的一些内容(如您所述)。一旦修复,你只需要从数据上下文类中删除这些东西。也许有人对这个问题有另一种(甚至更好)的解决方案。EF7具有从DBContextOptionsBuilder到EntityOptionsBuilder的新语法。以下是准备在命令行上使用的脚手架。以上就是C#学习教程:在Startup.cs中添加DbContextOptions而无需注册数据存储和共享的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——publicclassContentContext:DbContext{publicDbSetContents{get;放;}publicDbSetContentCategories{get;放;}protectedoverridevoidOnConfiguring(EntityOptionsBuilderoptionsBuilder){optionsBuilder.UseSqlServer("Data:DefaultConnection:ConnectionString");,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: