Wanted to use SQL with EF6 in my Prism MVVM WPF App. Since I am new to EF6 I first made a simple sample that works just fine. In this sample I use one module and have 3 folders for Views, ViewModels and Models, the latter holding the SQL stuff.
So I translated the concept to my Prism App. Here I have a different structure. A 'Root' Module with the bootstrapper in the root and the MainShell and its Viewmodel in Views and ViewModels folders Respectively. I than have a folder Modules which holds 2 projects. in project 'Data' I have my EF logic in the 'SQL' folder. And in the project 'MainView' I have the MainView and the MainViewModel in the Folders 'Views' and 'ViewModels' respectively. Now when I compile I get the following message: 'No connection string named 'Entities01' could be found in the application config file'.
I then added the configuration string to the configuration file (App.config) in both the 'Root' Module and the 'MainView' Module. I also updated the paths to reflect the new location. But I now get a different error message.
System.Data.Entity.Core.MetadataException: 'The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.'
To me this indicates that I did not change the path correctly.
Original (as created by EF6) connection string as follows:
<connectionStrings>
<add name="Entities01" connectionString="metadata=res://*/SQL.DB01.csdl|res://*/SQL.DB01.ssdl|res://*/SQL.DB01.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-14K5J6E\DB01;initial catalog=NLTrader01;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I than modified the above code to the following to reflect the new location:
<connectionStrings>
<add name="Entities01" connectionString="metadata=res://*/Data/SQL.DB01.csdl|res://*/Data/SQL.DB01.ssdl|res://*/Data/SQL.DB01.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-14K5J6E\DB01;initial catalog=NLTrader01;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I am at a loss as how to fix this. Is my approach just wrong or did I just make a typo in the new path?
I did add the using directives. I have also added EF6 to all projects. And I also added a reference to the 'Data' Project.
There are at least 2 answers to the above. Thanks to Evk for the first answer and guiding me in the direction for additional information.
My personal preference goes to the following answer.
<connectionStrings>
<add name="Entities01" connectionString="metadata=res://*/Data.SQL.DB01.csdl|res://*/Data.SQL.DB01.ssdl|res://*/Data.SQL.DB01.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-14K5J6E\DB01;initial catalog=NLTrader01;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
It is however also possible to use the following notation.
<connectionStrings>
<add name="Entities01" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-14K5J6E\DB01;initial catalog=NLTrader01;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I noticed that this solution is significantly slower than the first solution.
From the MS Documentation it is my understanding that there is yet another solution that should be slightly quicker again, but I have not figured that out yet, once I have I will add that to the solution.
There was an other issue that I had overlooked though.
When using Prism with the Prism ViewModelLocator you have to keep in mind that it is looking for the ViewModel in the folder 'ViewModels'. The workings of the ViewModelLocator are based on the assumption that the View is always located in the 'Views' Folder and the ViewModel is always located in the 'ViewModels' folder. And these 2 folders are again in the same root. So if you are using additional folders in order to organise your project, keep in mind that the NameSpace should not reflect the actual folder structure, but the structure as described above in order to make the ViewModelLocator work.