在Linux上运行 dnx . ef migration add FirstMigration 命令时,出现下面的错误:
Using context 'EfDbContext'. System.InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services. at Microsoft.Data.Entity.Storage.DataStoreSelector.SelectDataStore (ServiceProviderSource providerSource) <0x409990f0 + 0x001e0> in <filename unknown>:0 at Microsoft.Data.Entity.Internal.DbContextServices+<>c__DisplayClass6_0.<Initialize>b__0 () <0x40998c70 + 0x0003c> in <filename unknown>:0
请问如何解决这个问题?
是读取不到config.json配置文件引起的,连接字符串就写在config.json文件中。
之前Startup.cs是这么写的:
public Startup() { Configuration = new Configuration() .AddJsonFile("config.json"); }
改为下面的代码,问题就解决了:
public Startup(IApplicationEnvironment appEnv) { Configuration = new Configuration(appEnv.ApplicationBasePath) .AddJsonFile("config.json"); }
EF的配置代码如下:
public void ConfigureServices(IServiceCollection services) { services.AddEntityFramework() .AddSqlServer() .AddDbContext<EfDbContext>(options => { options.UseSqlServer(Configuration.Get("Data:ConnectionString")); }); }
相关链接:
Configuration constructor requires full path if relative paths are used to add Configuration files
没用过dnx,也没遇到过。搜到一篇文章说遇到过这个问题,可能您已经看过了:http://www.cnblogs.com/redmoon/archive/2015/06/22/4594139.html
试过了,这篇博文中的代码无法运行:
var builder = new ConfigurationBuilder() .AddJsonFile("config.json"); Configuration = builder.Build();
@dudu: 园豆居然还是给了我,哈哈,我是来打酱油的耶,:)