C# - How to get the directory path of a .NET Standard Library project inside a WPF solution

徘徊边缘 提交于 2020-06-01 05:30:32

问题


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:

  1. Even in WPF project it is not really creating a real SQLite db, it's is just creating a file MySQLite.db with size 0kb. And that is probably happening because all the db related files (dbContext, classes, etc) are in Standard Library project.
  2. And I finally noticed that PM>Update-Database -Project MyStandardLibraryProj NuGet 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!