locked
Putting E-F connection in DAL (Class library) App.config file also requires connection string in MVC web.config file too?? RRS feed

  • Question

  • User-734459410 posted

    I have a web app I'm working on that utilizes MVC 5 and E-F (version 6 I think).  Anyway, I moved my data objects (data connections and associated classes) to a DAL (data access layer) which I created as a new project in the same solution as the MVC app.  Since I'm using E-F, the connection string seems to be stored in the Application config file for the Class library.

    However, I have found, to get it to work properly with the DAL and the MVC app, I must also put the Db connection string into the web.config file for the MVc app.  Doesn't this sort of defeat the purpose of having a DAL. I've been working with MVC for about 2 years now but am sort of new to the DAL concept (I understand it's purposes and why people use them, as in my case, I may want to use the same data objects in another app, so I implemented the DAL).

    If this is not the correct way to do this, could I please get some assistance on how to get the DAL to work as it should and handle all database activities (connection string, data models, etc)?

    Wednesday, September 13, 2017 1:32 PM

Answers

  • User2053451246 posted

    Your separate DLL will not have any reference to the config file in the project it was created from.  That way the same DLL can point to different data sources without having to re-compile and distribute it.  It will always look at the config file of the project it's referenced from, by design, which is a good thing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 6:26 PM

All replies

  • Wednesday, September 13, 2017 6:12 PM
  • User2053451246 posted

    Your separate DLL will not have any reference to the config file in the project it was created from.  That way the same DLL can point to different data sources without having to re-compile and distribute it.  It will always look at the config file of the project it's referenced from, by design, which is a good thing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 13, 2017 6:26 PM
  • User-734459410 posted

    OK.  I sort of thought that the DAL would actually also contain any connection strings to the database, but I guess at that point, the DAL would then only be specific to that database connection or whatever connections were defined within the DAL.    But I guess all I see then is just the data models, and the E-F data models that were created when I implemented E-F into the DAL to communicate with an existing SQL Server database.  I guess maybe that's just the way it's supposed to be and I'm just not looking at it right (everything works fine, I'm just trying to understand the concepts).

    Wednesday, September 13, 2017 9:18 PM
  • User1120430333 posted

    OK.  I sort of thought that the DAL would actually also contain any connection strings to the database, but I guess at that point, the DAL would then only be specific to that database connection or whatever connections were defined within the DAL.    But I guess all I see then is just the data models, and the E-F data models that were created when I implemented E-F into the DAL to communicate with an existing SQL Server database.  I guess maybe that's just the way it's supposed to be and I'm just not looking at it right (everything works fine, I'm just trying to understand the concepts).

    I gave you the information to understand it. You still basically do not know the what,  when and why, when I clearly explained it in its simplistic layman terms in the WCF forum when another poster asked the same basic questions that you asked here in this forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7e248714-c742-4537-8137-741c737ca03b/deploying-a-wcf-service?forum=wcf

    <copied>

    An app.config is only used in debug runtime mode in Visual Studio.

    For IIS hosted solutions, the Web.config file is where .NET will look for any configuration information during application runtime. Any projects below the Web project, the root project, within the solution that have an app.config, the app.config information in the subordinate project must be copied to the web.config in the root project.

    If a project that creates a programname.exe file, a selfhosting exe, has an app.config, which is the root project, then another project within the solution that has an app.config, the app.config information in the subordinate project must be copied to the app.config in the root project.

    Why? It is because upon the successful build of solution, the root selfhosting exe project, VS will have its app.config copied over to a runtime config called programname.exe.config where .NET will look for config information at program runtime.

    The progamname.exe is deployed, and the progamname.exe.config must be deployed to the same location so .NET can find it.

    <end>

    Thursday, September 14, 2017 12:43 AM