升级到 .net 后连接到 Db
Connecting to Db after upgrading to .net
我编写了一个使用 LocalDb 的 WPF 程序。该程序与 |DataDirectory|
.
完美配合
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = @"(LocalDB)\MSSQLLocalDB",
AttachDBFilename = $"|DataDirectory|\Lc Db {userName}.mdf",
IntegratedSecurity = true
};
我从 .net46 升级到 .net5,但我仍然使用 EF 而不是 EF.Core。现在我在第一次访问数据库时遇到了这个异常:Invalid value for key 'attachdbfilename'
。如果我使用绝对路径,那么我可以到达 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
};
PureManApplicationDevelopment可以确定DataDirectory。在构造函数中将 _DataDir = string.Empty;
更改为 _DataDir = _CurrentPath;
然后您可以创建连接字符串。
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
};
我编写了一个使用 LocalDb 的 WPF 程序。该程序与 |DataDirectory|
.
var sqlConnectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = @"(LocalDB)\MSSQLLocalDB",
AttachDBFilename = $"|DataDirectory|\Lc Db {userName}.mdf",
IntegratedSecurity = true
};
我从 .net46 升级到 .net5,但我仍然使用 EF 而不是 EF.Core。现在我在第一次访问数据库时遇到了这个异常:Invalid value for key 'attachdbfilename'
。如果我使用绝对路径,那么我可以到达 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
};
PureManApplicationDevelopment可以确定DataDirectory。在构造函数中将 _DataDir = string.Empty;
更改为 _DataDir = _CurrentPath;
然后您可以创建连接字符串。
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
};