Configure more than one dbcontext in EF 7

c# entity-framework-core visual-studio-2015


I created a web starter application with VS 2015 ctp, and I would like to add an in-memory store to do some test, but when I try to read the data, I get this message

The data stores 'SqlServerDataStore' 'InMemoryDataStore' are available. A context can only be configured to use a single data store. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

How can I do to create a second datastore? Now I have this row in the ConfigureService method

  .AddDbContext<WorkModel>( options => { options.UseInMemoryStore(persist: true); });

Edit: Looks like the scenario is not clear. I have the identy sql server dbcontext, and I want to add a second dbcontext, totally separated, that I want to run in memory. I'm looking how configure two different dbcontext, in this case using two different datastores.

The first one is the Identity ApplicationDbContext, and another is something like this:

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);

Or whatever custom dbcontext do you like

3/16/2015 8:43:00 PM

Popular Answer

It's possible use one DbContext type with multiple data stores. It won't however play well with your calls to .AddDbContext(). Here is an example of how to do it taking .ConfigureServices() entirely out of the picture.

class MyContext : DbContext
    bool _useInMemory;

    public MyContext(bool useInMemory)
        _useInMemory = useInMemory;

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

You can then instantiate your context specifying which provider to use.

var inMemoryContext = new MyContext(useInMemory: true);
var sqlServerContext = new MyContext(useInMemory: false);
1/5/2016 10:36:27 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow