首页 新闻 会员 周边 捐助

EF7问题:No data stores are configured

0
悬赏园豆:20 [已解决问题] 解决于 2015-07-04 23:36

在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 

请问如何解决这个问题?

ef7
dudu的主页 dudu | 高人七级 | 园豆:31030
提问于:2015-07-04 20:46
< >
分享
最佳答案
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

dudu | 高人七级 |园豆:31030 | 2015-07-04 23:35
其他回答(1)
0

没用过dnx,也没遇到过。搜到一篇文章说遇到过这个问题,可能您已经看过了:http://www.cnblogs.com/redmoon/archive/2015/06/22/4594139.html

收获园豆:20
liqipeng | 园豆:1160 (小虾三级) | 2015-07-04 21:00

试过了,这篇博文中的代码无法运行:

var builder = new ConfigurationBuilder()
             .AddJsonFile("config.json");
Configuration = builder.Build();
支持(0) 反对(0) dudu | 园豆:31030 (高人七级) | 2015-07-04 22:18

@dudu: 园豆居然还是给了我,哈哈,我是来打酱油的耶,:)

支持(0) 反对(0) liqipeng | 园豆:1160 (小虾三级) | 2015-07-05 00:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册