0
votes

Is there a way to specify an "InMemory" database "connection string" (for lack of better terms) in the applicationsettings.json file?

I want to be able to use InMemory for development and then transform the connection for deployed (test, QA, prod) environments without having to change the code.

I recognize I can use a conditional statement to check and then go from there like -

var builder = new DbContextOptionsBuilder();


if(env.IsDevelopment())
{
    builder.UseInMemoryDatabase("DevelopmentContext");
}

I was just curious if there is more of a convention or api based way. I have not been able to find anything.

1
Depends on the provider. If you use SQLServer Provider, nope. If you use SQLite yea i think you can. Otherwise use an configurable entry in your appsettings.json which may be called "provider": "InMemory" or "provider": "SQLServer" and check for it (its conditionally too though). For SQLite check SQLite connection strings (second entry) - Tseng

1 Answers

0
votes

There is no way to do what you're asking. It can't be controlled by connection string alone; you must either use the in-memory provider or the SQL Server provider, for example, which means you must branch in code. Now, you could potentially make the provider a configuration value, and then use the config value to determine whether to use the in-memory provider vs your production provider, but that doesn't buy you anything that simply using IHostingEnvironment.IsDevelopment() doesn't.

That said, the in-memory provider should not actually be used even in development. That's not what it's for, and won't serve you as you imagine. The in-memory provider exists for testing, where volatility is not an issue as you'd want to tear down and rebuild the database after every test anyways. Using it in development wouldn't allow you reliably persist data from one request to the next, so you'd have problems with all sorts of things.

If you want a more simplistic data provider for development, you can use something like SQLite.