Replace connection string in Scaffold-DbContext - asp.net

I'm trying to learn by doing and it has been a fun struggle so far :D
I've finally got my project to scaffold my DB but it populates a warning in the code.
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("Server=<servername>;Database=<databaseName>;Trusted_Connection=True");
}
}
I've tried to update it to use the "DefalutConnection" in my appsettings.json file but it doesn't recognize Configuration. I've imported the namespace using Microsoft.Extensions.Configuration; but that still doesn't resolve the issue.
I'm trying to get something akin to the following: options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
Not sure which direction to go in resolving this. Is this a non-issue or am I missing adding something to get this to pull from appsettings.json?
My guess is that the DbCOntextOptionsBuilder doesn't have a method for Configuration and I would need to build it out myself... but I'm not sure how to do that either.
Thanks for your time/attention on this.
Solution Shown to Assist Others, See Marked Answer Below
Error
Unable to create an object of type 'ProfileContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
add public IConfiguration Configuration {get;}
inject it in your constructor
public ContextDbName(IConfiguration configuration)
{
Configuration = configuration;
}
update your OnConfiguring method
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(Configuration.GetConnectionString("DefaultConnection"));
}
}

I think you need to inject Configuration into your DbContext constructor.

Running with the original script, created by Scaffolding, in the newly created context file I ran into other issues regarding dependency injection. This got me thinking more about Microsofts Documents.
So I added an injection into my startup file for my newly created Context class and like magic, it worked :D
Startup.cs:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDbContext<ProfileContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));

You can use scaffold like this:
Scaffold-DbContext -Connection "This is the connectiostring" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data/Entities -ContextDir Data -Context "This is the contextname" -NoOnConfiguring -f
For your case -NoOnConfiguring will prevent to create the code with connectionstring (optionsBuilder.UseSqlServer)

Related

Xamarin Amazon IAP error using D8+R8 with Proguard in Release Failing but working Debug

I am using AmazonIapV2Android.dll provided by Amazon team for the Xamarin.Android project. I have implemented it last year and have been using successfully with Dx+proguard with using proguard rules as below. Those lines are also suggested by Amazon documentation. see the link
-dontwarn com.amazon.**
-keep class com.amazon.** {*;}
-keepattributes *Annotation*
Recently I have changed my xamarin.android project using d8+r8 using the same proguard file. Everything, google iap implementation also fine but Amazon IAP started throwing exception.
Jsonable.CheckForErrors
(System.Collections.Generic.Dictionary`2[TKey,TValue] jsonMap)
com.amazon.device.iap.cpt.AmazonException: java.lang.RuntimeException:
Missing type parameter.
at com.amazon.device.iap.cpt.RequestOutput.CreateFromJson
(System.String jsonMessage) [0x0002d] in
<26520843ea114e5a91256077e0412906>:0 \n at
com.amazon.device.iap.cpt.AmazonIapV2Impl+AmazonIapV2Base.GetProductData
(com.amazon.device.iap.cpt.SkusInput skusInput) [0x00013] in
I am using also linker as User and sdk assemblies, this is triggering obfuscation obviously and some methods are removed by the linker because using Sdk assemblies only or No Linking, everything works fine.
I have added the AmazonIapV2Android as linker to skip but it didnt help.
When I check the code implementation of the RequestOutput.CreateFromJson function implementation, it looks like as below.
using com.amazon.device.iap.cpt.json;
namespace com.amazon.device.iap.cpt
{
public sealed class RequestOutput : Jsonable
{
public string RequestId{get;set;}
public static RequestOutput CreateFromJson(string jsonMessage)
{
try
{
Dictionary<string, object> jsonMap = Json.Deserialize(jsonMessage) as Dictionary<string, object>;
Jsonable.CheckForErrors(jsonMap);
return CreateFromDictionary(jsonMap);
}
catch(System.ApplicationException ex)
{
throw new AmazonException("Error encountered while UnJsoning", ex);
}
}
and implementation for Jsonable in the dll looks as below
namespace com.amazon.device.iap.cpt
{
public abstract class Jsonable
{
public static Dictionary<string, object> unrollObjectIntoMap<T>(Dictionary<string, T> obj) where T:Jsonable
{
Dictionary<string, object> jsonableDict = new Dictionary<string, object>();
foreach (var entry in obj)
{
jsonableDict.Add (entry.Key, ((Jsonable)entry.Value).GetObjectDictionary());
}
return jsonableDict;
}
public static List<object> unrollObjectIntoList<T>(List<T> obj) where T:Jsonable
{
List<object> jsonableList = new List<object>();
foreach (Jsonable entry in obj)
{
jsonableList.Add(entry.GetObjectDictionary());
}
return jsonableList;
}
public abstract Dictionary<string, object> GetObjectDictionary();
public static void CheckForErrors(Dictionary<string, object> jsonMap)
{
object error;
if (jsonMap.TryGetValue("error", out error))
{
throw new AmazonException(error as string);
}
}
}
}
I have tried to use linker.xml with settings like below also but it didnt help either.
<assembly fullname="AmazonIapV2Android">
<namespace fullname="com.amazon.device.iap.cpt" />
<namespace fullname="com.amazon.device.iap.cpt.log" />
<namespace fullname="com.amazon.device.iap.cpt.json" />
</assembly>
I cannot figure out why should throw exception while i am defining keepclass for all methods and members under the namespace starting with com.amazon prefix.
Any idea what could be the reason here?
EDIT: just had several more tests and my initiale comment was slightly wrong. strange way app is working in debug with Linker set "SDK assemblies only" but in release it doesnt work even with "SDK assemblies only"
Obviously this is a known problem for using R8 and Amazon IAP. Typical amazon doesnt care and update their package. especially there is no update for Xamarin IAP since 2016.
Here are the links to problem
https://forums.developer.amazon.com/questions/205480/in-app-billing-not-working-since-android-studio-de.html
https://issuetracker.google.com/issues/134766810
Currently there are 3 workarounds,
disable r8. Bad is that no obfuscation, no optimization.
Use dx+proguard+multi dex instead of d8+r8. There is a problem here if you use androidx, androidx libraries dont work with dx+proguard, they work only with d8+r8, you need to go back to support libraries.
I am not sure but amazon website claims that it is claimed, it works with r8 but this is pobably for the android java library not for xamarin. Because as i cheked there is newer version to as jar. You can theoretically use Binding library to get a new dll and try but I read even for Android studio projects, this doesnt work. So i tried to create a binding library and it had many errors and api seems to be different than xamarin. It is a lot of effort for non-profitable app store.
here is the link to github issue on xamarin.android as well.

JpaSagaStore in conjunction with Jackson unable to properly store state

In a SpringBoot application, I have the following configuration:
axon:
axonserver:
servers: "${AXON_SERVER:localhost}"
serializer:
general: jackson
messages: jackson
events: jackson
logging.level:
org.axonframework.modelling.saga: debug
Downsizing the scenario to bare minimum, the relevant portion of Saga class:
#Slf4j
#Saga
#ProcessingGroup("AuctionEventManager")
public class AuctionEventManagerSaga {
#Autowired
private transient EventScheduler eventScheduler;
private ScheduleToken scheduleToken;
private Instant auctionTimerStart;
#StartSaga
#SagaEventHandler(associationProperty = "auctionEventId")
protected void on(final AuctionEventScheduled event) {
this.auctionTimerStart = event.getTimerStart();
// Cancel any pre-existing previous job, since the scheduling thread might be lost upon a crash/restart of JVM.
if (this.scheduleToken != null) {
this.eventScheduler.cancelSchedule(this.scheduleToken);
}
this.scheduleToken = this.eventScheduler.schedule(
this.auctionTimerStart,
AuctionEventStarted.builder()
.auctionEventId(event.getAuctionEventId())
.build()
);
}
#EndSaga
#SagaEventHandler(associationProperty = "auctionEventId")
protected void on(final AuctionEventStarted event) {
log.info(
"[AuctionEventManagerSaga] Current state: {scheduleToken={}, auctionTimerStart={}}",
this.scheduleToken,
this.auctionTimerStart
);
}
}
In the final compiled class, we will end up having 4 properties: log (from #Slf4j), eventScheduler (transient, #Autowired), scheduleToken and auctionTimerStart.
For reference information, here is a sample of the general approach I've been using for both Command and Event classes:
#Value
#Builder
#JsonDeserialize(builder = AuctionEventStarted.AuctionEventStartedBuilder.class)
public class AuctionEventStarted {
AuctionEventId auctionEventId;
#JsonPOJOBuilder(withPrefix = "")
public static final class AuctionEventStartedBuilder {}
}
When executing the code, you get the following output:
2020-05-12 15:40:01.180 DEBUG 1 --- [mandProcessor-4] o.a.m.saga.repository.jpa.JpaSagaStore : Updating saga id c8aff7f7-d47f-4616-8a96-a40044cb7e3b as {}
As soon as the general serializer is changed to xstream, the content is serialized properly, but I face another issue during deserialization, since I have private static final class Builder classes using Lombok.
So is there a way for Axon to handle these scenarios:
1- Axon to safely manage Jackson to ignore #Autowired, transient and static properties from #Saga classes? I've attempted to manually define #JsonIgnore at non-state properties and it still didn't work.
2- Axon to safely configure XStream to ignore inner classes (mostly Builder classes implemented as private static final)?
Thanks in advance,
EDIT: I'm pursuing a resolution using my preferred serializer: JSON. I attempted to modify the saga class and extend JsonSerializer<AuctionEventManagerSaga>. For that I implemented the methods:
#Override
public Class<AuctionEventManagerSaga> handledType() {
return AuctionEventManagerSaga.class;
}
#Override
public void serialize(
final AuctionEventManagerSaga value,
final JsonGenerator gen,
final SerializerProvider serializers
) throws IOException {
gen.writeStartObject();
gen.writeObjectField("scheduleToken", value.eventScheduler);
gen.writeObjectField("auctionTimerStart", value.auctionTimerStart);
gen.writeEndObject();
}
Right now, I have something being serialized, but it has nothing to do with the properties I've defined:
2020-05-12 16:20:01.322 DEBUG 1 --- [mandProcessor-0] o.a.m.saga.repository.jpa.JpaSagaStore : Storing saga id c4b5d94c-7251-40a5-accf-332768b1cacd as {"delegatee":null,"unwrappingSerializer":false}
EDIT 2 Decided to add more insight into the issue I experience when I switch general to use XStream (even though it's somewhat unrelated to the main issue described in the title).
Here is the issue it complains to me:
2020-05-12 17:08:06.495 DEBUG 1 --- [ault-executor-0] o.a.a.c.command.AxonServerCommandBus : Received command response [message_identifier: "79631ffb-9a87-4224-bed3-a957730dced7"
error_code: "AXONIQ-4002"
error_message {
message: "No converter available\n---- Debugging information ----\nmessage : No converter available\ntype : jdk.internal.misc.InnocuousThread\nconverter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter\nmessage[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not \"opens jdk.internal.misc\" to unnamed module #7728643a\n-------------------------------"
location: "1#600b5b87a922"
details: "No converter available\n---- Debugging information ----\nmessage : No converter available\ntype : jdk.internal.misc.InnocuousThread\nconverter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter\nmessage[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not \"opens jdk.internal.misc\" to unnamed module #7728643a\n-------------------------------"
}
request_identifier: "2f7020b1-f655-4649-bbe0-d6f458b3c2f3"
]
2020-05-12 17:08:06.505 WARN 1 --- [ault-executor-0] o.a.c.gateway.DefaultCommandGateway : Command 'ACommandClassDispatchedFromSaga' resulted in org.axonframework.commandhandling.CommandExecutionException(No converter available
---- Debugging information ----
message : No converter available
type : jdk.internal.misc.InnocuousThread
converter : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
message[1] : Unable to make field private static final jdk.internal.misc.Unsafe jdk.internal.misc.InnocuousThread.UNSAFE accessible: module java.base does not "opens jdk.internal.misc" to unnamed module #7728643a
-------------------------------)
Still no luck on resolving this...
I've worked on Axon systems where the only used Serializer implementation was the JacksonSerializer too. Mind you though, this is not what the Axon team recommends. For messages (i.e. commands, events and queries) it makes perfect sense to use JSON as the serialized format. But switching the general Serializer to jackson means you have to litter your domain logic (e.g. your Saga) with Jackson specifics "to make it work".
Regardless, backtracking to my successful use case of jackson-serialized-sagas. In this case we used the correct match of JSON annotations on the fields we desired to take into account (the actual state) and to ignore the one's we didn't want deserialized (with either transient or #JsonIgnore). Why both do not seem to work in your scenario is not entirely clear at this stage.
What I do recall is that the referenced project's team very clearly decided against Lombok due to "overall weirdnes" when it comes to de-/serialization. As a trial it thus might be worth to not use any Lombok annotations/logic in the Saga class and see if you can de-/serialize it correctly in such a state.
If it does work at that moment, I think you have found your culprit for diving in further search.
I know this isn't an exact answer, but I hope it helps you regardless!
Might be worthwhile to share the repository where this problems occurs in; might make the problem clearer for others too.
I was able to resolve the issue #2 when using XStream as general serializer.
One of the Sagas had an #Autowired dependency property that was not transient.
XStream was throwing some cryptic message, but we managed to track the problem and address it.
As for JSON support, we had no luck. We ended up switched everything to XStream for now, as the company only uses Java and it would be ok to decode the events using XStream.
Not the greatest solution, as we really wanted (and hoped) JSON would be supported properly out of the box. Mind you, this is in conjunction with using Lombok which caused for the nuisance in this case.

How to log Entity Framework Core operations by NLog

I would like to use NLog to log SQL queries from Entity Framework Core in a manner similar to WebApi Core. How can I set it up?
For logging with Entity Framework Core there are some docs here.
You need this: (see the docs)
public static readonly LoggerFactory MyLoggerFactory
= new LoggerFactory(new[] {new ConsoleLoggerProvider((_, __) => true, true)});
and use the NLogLoggerProvider instead of the ConsoleLoggerProvider, from this package: https://www.nuget.org/packages/NLog.Extensions.Logging
and something like this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseLoggerFactory(MyLoggerFactory) // Warning: Do not create a new ILoggerFactory instance each time
.UseSqlServer(
#"Server=(localdb)\mssqllocaldb;Database=EFLogging;Trusted_Connection=True;ConnectRetryCount=0");
also you need to load your NLog config file:
NLog.LogManager.LoadConfiguration("nlog.config");
Of course you need a nlog configuration (nlog.config or could be in C#), check https://github.com/NLog/NLog/wiki/Configuration-file for that.
Update: works well according the comments :)

EFCore SQLite connection string with relative path in asp.net

I have just added SQLite to my asp.net webApi project, and am having trouble working out how get the path to the App_Data folder to pass to DbContextOptionsBuilderUseSqlite
I have the following in the web.config I have a link to an external a config file with the conenction string...
<connectionStrings configSource="config\connectionStrings.config"/>
and in there I have...
<connectionStrings>
<add name="MyDatastore"
connectionString="DataSource=./App_Data/test.sqlite" />
</connectionStrings>
And in my DbContext.OnConfiguring I Have....
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string path = WebConfigurationManager.ConnectionStrings["MyDatastore"].ConnectionString;
optionsBuilder.UseSqlite(path);
}
}
The path is correctly retrieved (I can see I get the path as configured on connectionStrings.config
so ./App_Data/test.sqlite is passed to optionsBuilder.UseSqlite(path).
However, I get the following error...
SQLite Error 14: 'unable to open database file'.
If I use just connectionString="DataSource=test.sqlite" /> then it seems to magically find the file in the App_Data folder, when I ran on my dev machine in debug, but I had problems on another machine (release build). I assume it is the path, though all I get back is 'unable to open database file'.
I also tried..
connectionString="DataSource=|DataDirectory|test.sqlite" />
This gives me a Illegal characters in path error.
The following does work (full path)
connectionString="d:\0\test.sqlite" />
But I want to be able to use relative paths, eg maybe even .\datastore\test.sqlite.
Does any one have any ideas on this?
Thanks in advance
You'll have to fix up the relative paths at runtime:
var builder = new SqliteConnectionStringBuilder(connectionString);
builder.DataSource = Path.GetFullPath(
Path.Combine(
AppDomain.CurrentDomain.GetData("DataDirectory") as string
?? AppDomain.CurrentDomain.BaseDirectory,
builder.DataSource);
connectionString = builder.ToString();
Works perfectly for me.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dataSource = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "siteDB.db");
optionsBuilder
.UseSqlite($"Data Source={dataSource};");
}
Note: This solution was tested for .Net Core 5, and one can presume it will work on 2.x, 3.x, 5
If you want to use a diferent project than the one provided when you started, you have to specify the correct path ("Data Source = ..\\MyApplication.DAL\\sqliteDatabase.db") in the appsettings.json.
In this presented case, you don't even need to write the method OnConfiguring(DbContextOptionsBuilder optionsBuilder) in the ApplicationDbContext.cs.
You have a full setup bellow (Startup & appsettings.json).
My project structure:
-> MyApplication (solution)
-> MyApplication.UI (initial project of the solution)
-> MyApplication.BL (project)
-> MyApplication.DAL (project)
Inside Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//... other services
services.AddDbContext<ApplicationDbContext>
(x => x.UseSqlite(Configuration.GetConnectionString("SqliteConnection")));
//.... other services and logic
}
In appsettings.json :
"ConnectionStrings": {
"SqliteConnection": "Data Source = ..\\MyApplication.DAL\\sqliteDatabase.db"
}
Works for me on linux, .net core 5.
var builder = new SqliteConnectionStringBuilder("Data Source=MyDatabase.db");
builder.DataSource = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, builder.DataSource);
services.AddDbContext<MyContext>(o => o.UseSqlite(builder.ToString());
Assumes database is in the bin directory, e.g. MyProject/bin/Debug/MyDatabase.db or MyProject/bin/Release/MyDatabase.db.
If you are a .Net Core backend developer who use sqlite, make sure to use below code example. Otherwise SQLite Error 14: 'unable to open database file' error will come.
Startup.cs
var baseDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string dbPathSystemLog = Path.Combine(baseDirectory, "CAMSCoreSystemLog.db");
SystemLogDBContext.cs
public class SystemLogDBContext : DbContext
{
public SystemLogDBContext(DbContextOptions<SystemLogDBContext> options) : base(options)
{
Database.EnsureCreated();
}
}
This line will create the Db if not exist
Database.EnsureCreated();
I was struggling two days. This will help someone.

ASP.NET 5, EF 7 and SQLite - SQLite Error 1: 'no such table: Blog'

I followed the Getting Started on ASP.NET 5 guide about Entity Framework 7 and I replaced MicrosoftSqlServer with Sqlite, the only difference in the code is in Startup.cs:
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<BloggingContext>(options => options.UseSqlite("Filename=db.db"));
When I run the website and navigate to /Blogs, I get an error:
Microsoft.Data.Sqlite.SqliteException was unhandled by user code
ErrorCode=-2147467259 HResult=-2147467259 Message=SQLite Error 1:
'no such table: Blog' Source=Microsoft.Data.Sqlite
SqliteErrorCode=1 StackTrace:
at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc,
Sqlite3Handle db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior
behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior
behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.d__1`2.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EFGetStarted.AspNet5.Controllers.BlogsController.Index() in d:\arthur\documents\visual studio
2015\Projects\EFGetStarted.AspNet5\src\EFGetStarted.AspNet5\Controllers\BlogsController.cs:regel
18 InnerException:
I understand this as if there is no table called 'Blog', but when I open the .db file in DB Browser for SQLite, there actually is a table called 'Blog':
Does SQLite require other changes in the code, or is this an error in the SQLite connector for Entity Framework?
It is very likely the database actually being opened by EF is not the file you are opening in DB Browser. SQLite use the process current working directory, which if launched in IIS or other servers, can be a different folder than your source code directory. (See issues https://github.com/aspnet/Microsoft.Data.Sqlite/issues/132 and https://github.com/aspnet/Microsoft.Data.Sqlite/issues/55).
To ensure your db file is in the right place, use an absolute path. Example:
public class Startup
{
private IApplicationEnvironment _appEnv;
public Startup(IApplicationEnvironment appEnv)
{
_appEnv = appEnv;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<MyContext>(
options => { options.UseSqlite($"Data Source={_appEnv.ApplicationBasePath}/data.db"); });
}
}
I did this and was still having trouble loading the database. I added the following code in the constructor for the database context:
Database.EnsureCreated();
Now my context file looks like this:
It created a new database on my new Azure hosting site, so if you have a lot of existing data to migrate, this won't work. It worked for me so figured I'd share.
Taken from EF Core documentation...
Run from Visual Studio
To run this sample from Visual Studio, you must set the working
directory manually to be the root of the project. Ifyou don't set the
working directory, the following Microsoft.Data.Sqlite.SqliteException
is thrown: SQLite Error 1: 'no such table: Blogs'.
To set the working directory:
In Solution Explorer, right click the project and then select Properties.
Select the Debug tab in the left pane.
Set Working directory to the project directory.
Save the changes.
I had this issue on netcoreapp2.0. There's a related issue that may be at fault, but I didn't want to solve it by going to a nightly build.
The solution for me was to create and pass an SqliteConnection instead of using the builder string.
So for this setup:
string id = string.Format("{0}.db", Guid.NewGuid().ToString());
var builder = new SqliteConnectionStringBuilder()
{
DataSource = id,
Mode = SqliteOpenMode.Memory,
Cache = SqliteCacheMode.Shared
};
Compose for the DI like so:
var connection = new SqliteConnection(builder.ConnectionString);
connection.Open();
connection.EnableExtensions(true);
services.AddDbContext<SomeDbContext>(options => options.UseSqlite(connection));
The error I had was using this style of init:
services.AddDbContext<SomeDbContext>(options => options.UseSqlite(builder.ConnectionString));
My scaffolding also has a one-time call to:
var dbContext = serviceScope.ServiceProvider.GetService<SomeDbContext>();
dbContext.Database.OpenConnection();
dbContext.Database.EnsureCreated();
Using this approach all my DI-instantiated copies of SomeDbContext would all point at a valid SQLite db, and that db would have auto-created schema as per my entities.
Looks like things have changed because IApplicationEnvironment has been replaced with IHostingEnvironment.
Removing IApplicationEnvironment \ IRuntimeEnvironment
public class Startup
{
private IHostingEnvironment _appHost;
public Startup(IHostingEnvironment appHost)
{
_appHost = appHost;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlite()
.AddDbContext<MyContext>(
options => { options.UseSqlite($"Data Source={_appHost.ContentRootPath}/data.db"); });
}
}
I Had the same problem and I found the answer in this link:
https://www.youtube.com/watch?v=yRmMYrSROPs
Just update your database via command:
dotnet ef update database
you can set up the configuration adding;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source = path//DatabaseName.db");
}
the example uses SQLite, but you can choose your own DB.
If you have multiple DataContexts, the EnsureCreated method will only work on the first call. See its documentation:
If the database exists and has any tables, then no action is taken
To create tables of additional DataContexts, use
RelationalDatabaseCreator databaseCreator =
(RelationalDatabaseCreator) secondDbContext.Database.GetService<IDatabaseCreator>();
databaseCreator.CreateTables();

Resources