I write a WPF program which uses LocalDb. The program worked perfectly with |DataDirectory|
.
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = @"(LocalDB)\MSSQLLocalDB",
AttachDBFilename = $"|DataDirectory|\\Lc Db {userName}.mdf",
IntegratedSecurity = true
};
I upgraded from .net46 to .net5 but I still use EF and not EF.Core. Now I got this exception when reaching for the db at the first time: Invalid value for key 'attachdbfilename'
. If I use absolute path then I can reach the Db.
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = @"(LocalDB)\MSSQLLocalDB",
AttachDBFilename = $"AttachDBFilename = @"c:\Users\xyz\Documents\LedgerCommander\LedgerCommander\bin\Debug\net5.0-windows\Lc Db Heckl.mdf"",
IntegratedSecurity = true
};
How should I use |DataDirectory|
in .net core? I tried a number of approach without success. 1, 2, 3, 4
CodePudding user response:
The PureManApplicationDevelopment can determine the DataDirectory. In the constructor change _DataDir = string.Empty;
to _DataDir = _CurrentPath;
Then you can create the connection string.
var dataDirectory = PureManApplicationDeployment.DataDirectory;
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = @"(LocalDB)\MSSQLLocalDB",
//AttachDBFilename = $"|DataDirectory|\\Lc Db {userName}.mdf",
AttachDBFilename = $"{dataDirectory}Lc Db {userName}.mdf",
IntegratedSecurity = true
};