-1

In my project I'm using Entity Framework and from the start I created the Model from a local database. Now I want to use a .mdf file (in the project directory) instead of a local database, but I want to do this with as little interference in the project as possible.

If it is possible I don't want to create a new Model or changing the Entity name and so on. What is the most efficient way to do this? Can I just change the connection string? And if yes, what should I change it to?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brazol
  • 435
  • 1
  • 6
  • 17
  • What do you mean by local database? – Ladislav Mrnka Jun 04 '11 at 19:09
  • Hmm i mean database store locally on my computer the way i created it in SQL Server. The connection string to it now looks like this: `` I want to know how to change it to work with .mdf file stored in project dir instead. :) – Brazol Jun 04 '11 at 19:27

1 Answers1

0

Just copy the mdf file to your AppData directory and change the connection to look like this:

<add name="CONNECTION-NAME" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBM‌​odel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=NAME-OF-MY-COMPUTER\sqlexpress;AttachDbFilename=|DataDirectory|\MyDBFile.mdf;Initial Catalog=DATABASE-NAME;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Note this part: AttachDbFilename=|DataDirectory|\MyDBFile.mdf;

Shady M. Najib
  • 2,151
  • 2
  • 19
  • 30
  • Thx for answer but 1. it doesnt work :/ when i try to connect to db the exception is thrown: {"Unable to load the specified metadata resource."} 2. my goal is to be able to run project from another computer therefore i wanted to "attach" database to project and store it in it. But will it work when in the connection string you wrote there is still info about my computer and it still connects to sqlexpress on my computer? Sorry if my questions are trivial but im beginner at this. – Brazol Jun 04 '11 at 19:57
  • it's always possible to write your data source as: "Data Source=.\sqlexpress;" which works on whatever machine you're on.. not sure about the metadata thing though.. try to double click it in VS to open it in the server explorer – Shady M. Najib Jun 04 '11 at 20:55