0

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

Istvan Heckl
  • 864
  • 10
  • 22
  • Maybe you can try ErikEJ.EntityFramework.SqlServer which uses Microsoft.Data.SqlClient ? – ErikEJ Nov 26 '21 at 10:20
  • Thanks @ErikEJ I installed the nuget, changed the using, added the attribute to the dbContext. Now there is an error "The type 'SqlFunctions' exists in both 'EntityFramework.SqlServer, and 'ErikEJ.EntityFramework.SqlServer". I thought I should remove the nuget EntityFramework.SqlServer but actually there is no such nuget only the EntityFramework. Any idea? – Istvan Heckl Nov 26 '21 at 11:27
  • Interesting, can you share how you are using SqlFunctions on Github? – ErikEJ Nov 26 '21 at 13:27
  • Dear @ErikEJ Here is the [link](https://github.com/heckl/learn-git.git). I created a method to replace a Db view. As I understand this compile error means that `SqlFunctions` is now in two assemblies. I tried to remove the old assembly using solution explorer. – Istvan Heckl Nov 26 '21 at 14:02
  • Most likely a bug, will investigate – ErikEJ Nov 26 '21 at 22:44
  • I have published an updated Nuget package with a fix for this, you have to rename SqlFunctions => MicrosoftSqlFunctions with the new package – ErikEJ Nov 27 '21 at 07:03
  • Thank you @ErikEJ I had to select the prerelease to install the latest version but now the compile time error is gone. Unfortunately, this did not help with the original issue. I still have the exception if `DataDirectory` is used. What is strange that the exception also says that the source is still `System.Data.SqlClient`. – Istvan Heckl Nov 27 '21 at 08:31
  • Check the readme, update your using! – ErikEJ Nov 27 '21 at 12:02
  • Dear @ErikEJ I have already set the attribute `DbConfigurationType` before the `dbContext`. Called `DbProviderFactories.RegisterFactory`, changed to `using Microsoft.Data.SqlClient;` Still I have the exception from `System.Data.SqlClient`. – Istvan Heckl Nov 27 '21 at 16:38
  • Please post an issue on Github with repro, and let us stop the discussion here – ErikEJ Nov 27 '21 at 20:02

1 Answers1

0

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
            };
Istvan Heckl
  • 864
  • 10
  • 22