简体   繁体   中英

Add in memory store for ASP.NET 5 application

I'm trying to add an in memory store for my ASP.NET 5 application.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddEntityFramework()
        .AddInMemoryStore()
        .AddDbContext<MyDbContext>();
}

Though I'm getting the following error:

$ dnx . kestrel
* Assertion at loader.c:1189, condition `!mono_loader_get_last_error ()' not met

Stacktrace:

  at <unknown> <0xffffffff>
  at Nalie.Startup.ConfigureServices (Microsoft.Framework.DependencyInjection.IServiceCollection) <0x0003a>
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <0xffffffff>
  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0xffffffff>
* Assertion at loader.c:1912, condition `!mono_loader_get_last_error ()' not met

Abort trap: 6

You need to update the AddDbContext call to specify which provider to use.

AddDbContext<MyDbContext>(options => options.UseInMemoryDatabase());

The call to AddInMemory just setups up the services required for that data store in DI, but you still need to tell the context which provider to use. This becomes clearer if you see something a little more complex.

services.AddEntityFramework() .AddInMemoryStore() .AddSqlServer() .AddDbContext<MyDbContext>(options => options.UseInMemroryDatabase()) .AddDbContext<MyOtherDbContext>(options => options.UseSqlServer("<connection string>"));

InMemory is a little unique in that it doesn't require any configuration to be used (unlike other providers that require a connection string). So we did consider making InMemory just be selected by default if it is the only provider registered... but that seemed very much like a special case that would just hinder folks understanding of how things work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM