7

I am using POCO approach on all my projects using Entity Framework. It's all good but I have one problem: It is creating database on my SQL Express, instead I want it to store my database in *.mdf file in App_Data folder.

1 Answer 1

7

Set an appropriate connection string in Web.config that makes use of the AttachDBFilename keyword.

<connectionStrings>
  <add name="MyContextClassName"
       connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True;MultipleActiveResultSets=True;AttachDBFilename=|DataDirectory|MyDatabase.mdf;User Instance=True"
       providerName="System.Data.SqlClient" />
</connectionStrings>
Sign up to request clarification or add additional context in comments.

6 Comments

But the thing is that database is not created in my App_Data folder. It's created straight in SQLEXPRESS
@Stanislav Did you try the answer? "AttachDBFilename=|DataDirectory|..." will create the database file inside of App_Data.
I tried and it showed me Exception has been thrown by the target of an invocation. when I did update-database -f -v.
@Stanislav Do you have any additional information? Does $Errors[0].Exception.InnerException.ToString() tell your more?
As it happends I upgraded to Win8 and now I don't even have SQLEXPRESS installed. Now it does what it is suppose to do - storing everything in file w/o even using database server.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.