问题
I have several projects: DAL, Business Layer, Domain Model, Client and a couple of others. The client project is set as the startup project and has the connection strings defined in its app.config. When I initialize a C# Interactive window with my project, it seems to initialize properly. There is a long list of #r references and I am left at a prompt. When I try to instantiate an object that has to read from the database in its constructor, I get "No connection string named "Entities" could be found in the application config file."
Which app.config is used when using C# interactive? I've seen posts saying that the startup project has to be the one with the connection strings in app.config. That is how it is setup, but it's not working.
I also saw the post where someone suggests creating a partial class to initialize an instance of the DBContext, but I am not interacting with the DBContext, directly, in the interactive window, so I'm not sure that will help.
Is there a separate app.config for the C# Interactive window or a way to specify which app.config to use? Most of the posts I've read that talk about this don't seem to have an working solution, but they are also pretty outdated. I am hoping there is a solution that I am just not finding.
回答1:
By querying which app.config was active in the interactive window, itself:
Print(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
I found this path:
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\ManagedLanguages\VBCSharp\InteractiveComponents\InteractiveHost32.exe.config"
Adding my connection string to that config file worked. EF started functioning, as expected, after that. You still have to initialize the interactive window from your project, of course. Just remember to clean up after yourself, when you are finished.
来源:https://stackoverflow.com/questions/55308861/project-cant-find-my-ef-connection-string-in-c-sharp-interactive