Robert Jaskowski
Robert Jaskowski

Reputation: 823

ASP.NET MVC 5 change database name

Hy!

I'm new to ASP.NET MVC 5. I try to change the default database name. But I don't know how. My goal is to have only one database for the whole application.

My actual working Web.Config:

    <connectionStrings>
        <add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-SqlTest-20131122100021.mdf;Initial Catalog=aspnet-SqlTest-20131122100021;Integrated Security=True"
         providerName="System.Data.SqlClient" />
    </connectionStrings>

This Web.Config file should look like this and should work ^^ (actual I get a error that the database could not be created and the physical name may be incorrect)

    <connectionStrings>
        <add name="DefaultConnection" connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=Example.mdf;Initial Catalog=Example;Integrated Security=True"
             providerName="System.Data.SqlClient" />
    </connectionStrings>

Thanks for help :)

Upvotes: 1

Views: 2553

Answers (1)

Lin
Lin

Reputation: 15188

You need to add |DataDirectory|\ before Example.mdf, like below:

 <connectionStrings>
    <add name="DefaultConnection" 
    connectionString="DataSource=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Example.mdf;Initial Catalog=Example;Integrated Security=True"
     providerName="System.Data.SqlClient" />
</connectionStrings>

Upvotes: 4

Related Questions