0

我用 VS 2015 ctp 创建了一个 web 启动器应用程序,我想添加一个内存存储来做一些测试,但是当我尝试读取数据时,我收到了这条消息

数据存储“SqlServerDataStore”“InMemoryDataStore”可用。只能将上下文配置为使用单个数据存储。在设置服务时,通过覆盖 DbContext 类或 AddDbContext 方法中的 OnConfiguring 来配置数据存储。

如何创建第二个数据存储?ConfigureService现在我在方法中有这一行

AddEntityFramework(Configuration)
  .AddSqlServer()
  .AddDbContext<ApplicationDbContext>()
  .AddInMemoryStore()
  .AddDbContext<WorkModel>( options => { options.UseInMemoryStore(persist: true); });

编辑:看起来情况尚不清楚。我有身份 sql server dbcontext,我想添加第二个 dbcontext,完全分开,我想在内存中运行。我正在寻找如何配置两个不同的 dbcontext,在这种情况下使用两个不同的数据存储。

第一个是 Identity ApplicationDbContext,另一个是这样的:

public class WorkModel : DbContext
{

    public DbSet<Cliente> Clienti { get; set; }
    public DbSet<Commessa> Commesse { get; set; }

    protected override void OnModelCreating(ModelBuilder builder) {
        builder.Entity<Cliente>().Key(cli => cli.ClienteId);
        builder.Entity<Commessa>().Key(cli => cli.CommessaId);
    }
}

或者您喜欢的任何自定义 dbcontext

4

4 回答 4

2

可以将一种DbContext类型与多个数据存储一起使用。但是,它不能很好地配合您对.AddDbContext(). 这是一个如何.ConfigureServices()完全脱离图片的示例。

class MyContext : DbContext
{
    bool _useInMemory;

    public MyContext(bool useInMemory)
    {
        _useInMemory = useInMemory;
    }

    protected override void OnConfiguring(DbContextOptions options)
    {
        if (_useInMemory)
        {
            options.UseInMemoryStore(persist: true);
        }
        else
        {
            options.UseSqlServer();
        }
    }
}

然后,您可以实例化您的上下文,指定要使用的提供程序。

var inMemoryContext = new MyContext(useInMemory: true);
var sqlServerContext = new MyContext(useInMemory: false);
于 2015-03-16T16:10:25.930 回答
1

你可能需要做这样的事情而不是......

// Add first DbContext here
AddEntityFramework(Configuration)
  .AddSqlServer()
  .AddDbContext<ApplicationDbContext>();

// Add second DbContext here
AddEntityFramework(Configuration)
 .AddInMemoryStore()
 .AddDbContext<WorkModel>(options => { options.UseInMemoryStore(persist: true); });

就我而言,当我需要创建两个 SQL 上下文时,我必须这样做......

AddEntityFramework(Configuration)
  .AddSqlServer()
  .AddDbContext<ApplicationDbContext>()
  .AddDbContext<AnotherDbContext>();
于 2015-04-06T18:54:02.883 回答
0

如果你的 dbcontext 有

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
        #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
        optionsBuilder.UseSqlServer(<connection string>);
}

那么您可以通过添加防护来避免双重配置

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if(!optionsBuilder.IsConfigured){
        #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
        optionsBuilder.UseSqlServer(<connection string>);
    }
}

然后有一个扩展你的 DBContext 的类,它使用内存提供程序,并为你的测试显式实例化。然后你可以传入目标类,避免双重提供者问题。

于 2017-08-16T19:16:30.193 回答
0

就我而言,我将 Entity Framework Core 1.0.1 与 ASP.NET Core 1.0.1 一起使用,并且我想在 Windows 和 OS X 上运行相同的项目,但使用不同的数据库。所以这对我有用:

在中添加了一些逻辑Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("WindowsConnection")));
    }
    else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(Configuration.GetConnectionString("OSXConnection")));
    }
}

然后在中定义了两个不同的连接字符串appsettings.json

"ConnectionStrings":
{
  "WindowsConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-{dbname}-{dbguid};Trusted_Connection=True;MultipleActiveResultSets=true",
  "OSXConnection": "Data Source=\/Users\/{username}\/Documents\/{projectname}\/bin\/debug\/netcoreapp1.0\/{dbname}.db"
}

并将此逻辑添加到ApplicationDbContext.cs

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
    {
        optionsBuilder.UseSqlite("Filename=./{dbname}.db");
    }
}
于 2016-09-19T01:58:41.400 回答