问题
I have a VS2019 solution that has a WPF project referencing a .NET Standard 2.0 project included in the same solution. I am using Entity Framework Core on the .NET Standard Library project where the DbContext class is created as follows.
Because the MyDbContext.cs file is inside the Standard Library project, I was expecting AppDomain.CurrentDomain.BaseDirectory directory in the code below to be inside\bin\Debug\ of the Standard Library project where MySQLite.db should have been created . But, on the contrary, it's creating the db inside \bin\Debug\ of WPF project.
Question: In the following code how can we make sure the db is created inside Standard Library project's installation directory (bin\Debug) and not inside the startup project `WPF's directory?
Remark:
- Even in
WPFproject it is not really creating a real SQLite db, it's is just creating a fileMySQLite.dbwith size0kb. And that is probably happening because all the db related files (dbContext, classes, etc) are in Standard Library project. - And I finally noticed that
PM>Update-Database -Project MyStandardLibraryProjNuGet command actually is creating the real MySQLite.db with all the tables etc. at a strange location:C:\Users\MyUserName\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\3.1.4\tools\net461\any\MySQLite.db
MyDbContext.cs file inside .NET Standard Library Project:
……...
using Microsoft.EntityFrameworkCore;
…...
namespace MyStandardLibraryProj
{
public class MyDbContext : DbContext
{
string sSQLiteDbPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "MySQLiteDb.db");
public DbSet<MyObject> MyObjects{ get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source = {sSQLiteDbPath}");
}
}
来源:https://stackoverflow.com/questions/61968189/c-sharp-how-to-get-the-directory-path-of-a-net-standard-library-project-insid