none
Entity framework connection string RRS feed

  • Question

  • Hi All,

    I am wondering if there is a way to set the connection string for Entity Framework in one place so we don't have to have multiple App.config files and also allows use to use 3rd party tools for testing against. we don't want to set it in the DBContext as we may need to change it later on and dont want to have to recompile any code and deploy just change it in a config file.

    I have tried a few things such as setting it through configuration manager but i keep getting a not set to an object error?

    Hope someone can get me on the right track with this.

    Regards

    Aidan

    • Moved by Kristin Xie Monday, February 2, 2015 2:18 AM move to appropriate forum
    Friday, January 30, 2015 9:25 PM

Answers

  • Hello Aidan,

    For the requirement, my suggestions are:

    One is that you could store your connection string to a .txt file and in your project, you could read the .txt file to obtain the connection string and pass it to the DbContext class(you need to create a an additional constructor which accepts an argument), then you just need to configure the file path(you could also provide the file in your project directly so that you do not need to configure it in every app.config file) and you could change the connection string as you want whenever.

    Another is that you could place the connection string to the machine which is the root config file.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 2:32 AM
    Moderator

All replies

  •  

    Hi Aidan,

    Like your title mentioned, your case related to EF, i will move your thread to ADO.NET Entity Framework and LINQ to Entities forum for better support.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 2:17 AM
  • Hello Aidan,

    For the requirement, my suggestions are:

    One is that you could store your connection string to a .txt file and in your project, you could read the .txt file to obtain the connection string and pass it to the DbContext class(you need to create a an additional constructor which accepts an argument), then you just need to configure the file path(you could also provide the file in your project directly so that you do not need to configure it in every app.config file) and you could change the connection string as you want whenever.

    Another is that you could place the connection string to the machine which is the root config file.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 2, 2015 2:32 AM
    Moderator