How would I alter my code-first ASP.NET MVC app to make my database location relative to my app home...












0















This is an off-shoot of another question I asked, which one person suggested that in order for the SQL Server database to be accessible to my instructor when I send him my code-first app, I should "have a database location which is relative to [my] application home directory."



So I googled that exact phrase, and have not found steps on how to do that which seem relevant to my situation. This might be simpler than I'm making it, but what exactly do I have to adjust in order to make the database location relative? Does this have to do with the connection string in web.config?



My connection string is as follows, with StudentContextDB being the database that gets auto-generated by running the app:



<connectionStrings>
<add name="StudentContext"
providerName="System.Data.SqlClient"
connectionString="Data Source=DESKTOP-P3ECVONSQLEXPRESS;Initial Catalog=StudentContextDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|StudentContextDB.mdf" />
</connectionStrings>


DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?



Again, I apologize if this is exceedingly obvious and I'm missing it. Thanks in advance for any suggestions or guidance!










share|improve this question





























    0















    This is an off-shoot of another question I asked, which one person suggested that in order for the SQL Server database to be accessible to my instructor when I send him my code-first app, I should "have a database location which is relative to [my] application home directory."



    So I googled that exact phrase, and have not found steps on how to do that which seem relevant to my situation. This might be simpler than I'm making it, but what exactly do I have to adjust in order to make the database location relative? Does this have to do with the connection string in web.config?



    My connection string is as follows, with StudentContextDB being the database that gets auto-generated by running the app:



    <connectionStrings>
    <add name="StudentContext"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=DESKTOP-P3ECVONSQLEXPRESS;Initial Catalog=StudentContextDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|StudentContextDB.mdf" />
    </connectionStrings>


    DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?



    Again, I apologize if this is exceedingly obvious and I'm missing it. Thanks in advance for any suggestions or guidance!










    share|improve this question



























      0












      0








      0








      This is an off-shoot of another question I asked, which one person suggested that in order for the SQL Server database to be accessible to my instructor when I send him my code-first app, I should "have a database location which is relative to [my] application home directory."



      So I googled that exact phrase, and have not found steps on how to do that which seem relevant to my situation. This might be simpler than I'm making it, but what exactly do I have to adjust in order to make the database location relative? Does this have to do with the connection string in web.config?



      My connection string is as follows, with StudentContextDB being the database that gets auto-generated by running the app:



      <connectionStrings>
      <add name="StudentContext"
      providerName="System.Data.SqlClient"
      connectionString="Data Source=DESKTOP-P3ECVONSQLEXPRESS;Initial Catalog=StudentContextDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|StudentContextDB.mdf" />
      </connectionStrings>


      DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?



      Again, I apologize if this is exceedingly obvious and I'm missing it. Thanks in advance for any suggestions or guidance!










      share|improve this question
















      This is an off-shoot of another question I asked, which one person suggested that in order for the SQL Server database to be accessible to my instructor when I send him my code-first app, I should "have a database location which is relative to [my] application home directory."



      So I googled that exact phrase, and have not found steps on how to do that which seem relevant to my situation. This might be simpler than I'm making it, but what exactly do I have to adjust in order to make the database location relative? Does this have to do with the connection string in web.config?



      My connection string is as follows, with StudentContextDB being the database that gets auto-generated by running the app:



      <connectionStrings>
      <add name="StudentContext"
      providerName="System.Data.SqlClient"
      connectionString="Data Source=DESKTOP-P3ECVONSQLEXPRESS;Initial Catalog=StudentContextDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|StudentContextDB.mdf" />
      </connectionStrings>


      DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?



      Again, I apologize if this is exceedingly obvious and I'm missing it. Thanks in advance for any suggestions or guidance!







      sql-server asp.net-mvc code-first






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 8:56









      marc_s

      580k13011191266




      580k13011191266










      asked Jan 1 at 8:26









      FreddieMercuryFreddieMercury

      668




      668
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You can write your connection string as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=(LocalDB)mssqlLocalDb;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />


          According to your following question,




          DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?




          if you want to use SQL Server Express Database then use dot(.) instead of hardly typed server name as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />


          This will add the DatabaseFileName.mdf file to the App_Data folder of the project.



          For more details: SQL Server Connection Strings for ASP.NET Web Applications






          share|improve this answer


























          • Thank you @TanvirArjel! This did it perfectly for me!

            – FreddieMercury
            Jan 1 at 21:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994044%2fhow-would-i-alter-my-code-first-asp-net-mvc-app-to-make-my-database-location-rel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You can write your connection string as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=(LocalDB)mssqlLocalDb;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />


          According to your following question,




          DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?




          if you want to use SQL Server Express Database then use dot(.) instead of hardly typed server name as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />


          This will add the DatabaseFileName.mdf file to the App_Data folder of the project.



          For more details: SQL Server Connection Strings for ASP.NET Web Applications






          share|improve this answer


























          • Thank you @TanvirArjel! This did it perfectly for me!

            – FreddieMercury
            Jan 1 at 21:48
















          2














          You can write your connection string as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=(LocalDB)mssqlLocalDb;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />


          According to your following question,




          DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?




          if you want to use SQL Server Express Database then use dot(.) instead of hardly typed server name as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />


          This will add the DatabaseFileName.mdf file to the App_Data folder of the project.



          For more details: SQL Server Connection Strings for ASP.NET Web Applications






          share|improve this answer


























          • Thank you @TanvirArjel! This did it perfectly for me!

            – FreddieMercury
            Jan 1 at 21:48














          2












          2








          2







          You can write your connection string as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=(LocalDB)mssqlLocalDb;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />


          According to your following question,




          DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?




          if you want to use SQL Server Express Database then use dot(.) instead of hardly typed server name as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />


          This will add the DatabaseFileName.mdf file to the App_Data folder of the project.



          For more details: SQL Server Connection Strings for ASP.NET Web Applications






          share|improve this answer















          You can write your connection string as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=(LocalDB)mssqlLocalDb;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />


          According to your following question,




          DESKTOP-P3ECVON is my server name. How can I make the DB location relative, or otherwise make it so anyone who downloads this app can run it properly and have the database be generated on their own computer?




          if you want to use SQL Server Express Database then use dot(.) instead of hardly typed server name as follows:



          <add name="ConnectionStringName"
          providerName="System.Data.SqlClient"
          connectionString="Data Source=.SQLEXPRESS;AttachDbFileName=|DataDirectory|DatabaseFileName.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" />


          This will add the DatabaseFileName.mdf file to the App_Data folder of the project.



          For more details: SQL Server Connection Strings for ASP.NET Web Applications







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 1 at 10:41

























          answered Jan 1 at 10:34









          TanvirArjelTanvirArjel

          8,72432046




          8,72432046













          • Thank you @TanvirArjel! This did it perfectly for me!

            – FreddieMercury
            Jan 1 at 21:48



















          • Thank you @TanvirArjel! This did it perfectly for me!

            – FreddieMercury
            Jan 1 at 21:48

















          Thank you @TanvirArjel! This did it perfectly for me!

          – FreddieMercury
          Jan 1 at 21:48





          Thank you @TanvirArjel! This did it perfectly for me!

          – FreddieMercury
          Jan 1 at 21:48




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994044%2fhow-would-i-alter-my-code-first-asp-net-mvc-app-to-make-my-database-location-rel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory