0

In a current Project of mine I am using Entity Framework Core together with SQLite in an ASP.Net-Project.

My Problem is, that the Database in the project-files is used, instead of the one in the ./bin-Directory

I followed the instructions of the docs.microsoft-page to create the database:

https://learn.microsoft.com/de-de/ef/core/get-started/overview/first-app?tabs=visual-studio

This is the Connectionstring I am using.

protected override void OnConfiguring(DbContextOptionsBuilder options)
    => options.UseSqlite("Data Source=MySqliteDatabase.db");

I let the created Database be copyied over, when newer.

I browsed through a number of pages, looking for ways to provide a relative Database path, but I couldnt find a solution to this. It either still uses the project-DB-File, or it wont create the new Database, because it cant be opened, or so.

Does anyone have a solution to this? I noticed that Entity-Framework-Core-5 is kind of a new release, could this be a bug or similar of this particular version?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
BotMaster3000
  • 472
  • 4
  • 16
  • Use a full path in the connection string – ErikEJ Jan 03 '21 at 11:10
  • @ErikEJ Is this really the only way? How would I specify between a Debug- and a Release-Build then for example and what about git, when someone else pulls it to a different path? Wouldnt it break then? – BotMaster3000 Jan 03 '21 at 14:39
  • Did you try to put connection string in a config file? – Serge Jan 03 '21 at 17:06
  • @Sergey Which config-file are you referring to? – BotMaster3000 Jan 03 '21 at 20:26
  • I mean something like appsettings.json or app.config or web.config (I don't know what version net you use). – Serge Jan 03 '21 at 20:36
  • @Sergey I am not sure how to alter these. I just found out though, that Directory.GetCurrentDirectory also returns the path to the Project-Directory, instead of the /bin/debug/-Directory.. My current guess now is that there is something wrong in general, altough I am not sure what.. – BotMaster3000 Jan 03 '21 at 20:39
  • 1
    It is allways more reliable if you put the full patch as @ErikEJ advises. You can get this path from config file – Serge Jan 03 '21 at 20:40
  • @Sergey I got it now, the tip with the path from the config-file did it for me, I formulated an awnser based on my solution. Thanks alot – BotMaster3000 Jan 03 '21 at 20:59

1 Answers1

0

Thanks @Sergey and @ErikEJ.

So to solve this, I really needed to provide the full path of the directory. Since Directory.GetCurrentDirectory returned the wrong path, that is, the path to the project-directory instead of the /bin/Debug/... , I looked at the path that is named in the Config-File, as @Sergey suggessted, using the following Code:

AppDomain.CurrentDomain.SetupInformation.ApplicationBase

I got this from the following Stackoverflow page: How to find path of active app.config file?

It said there, to access the ConfigurationFile-Property, but for ASP.Net it is ApplicationBase I guess.

In there was the correct path, the one of the /bin/Debug/... .

Put together, my new Method looked like this:

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    string databasePath = $"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}MySqliteDatabase.db";
    options.UseSqlite($"Data Source={databasePath}");
}

This works for me as intended, thanks alot for the support, maybe this will help someone else as well.

BotMaster3000
  • 472
  • 4
  • 16