1

I'm following the book EF Code First by Lerman, and one of the things you do in the book is create a class library project in which you define a class that will inherit from DbContext. For this, you need to install Entity Framework. When you do this, two files will be added: App.config and packages.config.

Then you add a console project, and use your brand new context class in a using statement. You'll encounter quite some errors, because the Console Project does not have a reference to EntityFramework.dll and EntityFramework.SqlServer.dll, and also because I'm not using SQL Server Express, but a full version.

Now there are two options. The first is to also install entity framework in the console project. The other option is to reference the two .dll's, by adding a reference and adding them from the recent tab.

The first option will again add the App.config and packages.config files, but this time to the console project.

The second option does not. But trying to run the application will throw an exception, since it is looking for a connection string, and it can't find any. The solution I found is to copy the App.config that's in the class library project over to the console project, and put in a connectionString section in the copied App.config.

But this raises the question:

Which of these files can I remove? I seem to be able to remove the packages.config and App.config from the DataAccess project without any troubles. The console project seems to need the App.config.

3
  • How else would your console application know how to connect to a database if it did not have it's own app.config with a connection string in it?
    – Chris
    Jul 12, 2014 at 16:01
  • @Chris I think my post didn't come over as I intended. It's not about the console application having its own app.config file. I know it needs it. But the thing is this: I installed EF in a library project. This will also add two files, namely App.config and package.config. The console application also needs App.config. I have two options: Either install Entity Framework in both projects, or copy the App.config and the necessary DLL's to the console application. Either way, I'll App.config in two projects, the package.config in at least one, and I want to know which ones I can remove. Jul 12, 2014 at 16:11
  • The Console application will generate an .exe file which will refer to its own configuration file, hence this is where you should put the necessary configuration. AFAIK there is no possibility to merge configurations between a project and its dependencies. But you can mitigate the "issue" by completely isolating the library using the repository pattern. The configuration issue will remain but in a production environment you often use custom network shared configurations so you don't use the local app.config files either.
    – Pragmateek
    Jul 12, 2014 at 17:49

1 Answer 1

3

While you don't need the app.config in the library project per se if you're using code first (you do if you're using model or database first).. every time you add a project or do something that needs configuration, a new one will be added, so you might as well just keep it there.

You should keep the packages.config because this keeps track of the version of packages you have installed, so that you can more easily upgrade later.

If you're using database or model first, the designer needs the app.config to tell it the connection string to use.

4
  • Right. So you're saying: leave the App.config and package.config in every project that you'll need Entity Framework. With that, I probably be better off installing EF in a project instead of copying the DLL's over to projects that need them? Jul 13, 2014 at 9:23
  • Packages.config keeps track of what version of NuGet packages are installed for and referenced by the project. If you have no NuGet packages, you don't need it.
    – Suncat2000
    Nov 19, 2021 at 19:44
  • @Suncat2000 - Entity Framework is distributed through nuget. So not sure what your point is.. the project uses EF, so needs a packages.config to manage the version of the package. Nov 19, 2021 at 21:02
  • @ErikFunkenbusch NuGet is only one way to include libraries in your project - it is certainly not the only way nor always the best way. Entity Framework is distributed multiple ways. It wasn't even available to be installed separately from .NET until version 4.1. My point is: if you don't use NuGet, you don't need packages.config. Sadly, your point about app.config is valid; it causes more problems to remove it than to keep it.
    – Suncat2000
    Dec 14, 2021 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.