Newbie: how to execute MyData.edmx.sql

I created a simple Entity Framework model application in VS 2012 by creating an MVC 4 project and adding a single data object using the constructor.

The first time I right-click on the surface of the designer and select "Create Database", it will offer me data about the connection to the database. I suggest using the local database file in the App_data folder. When this is done, he created my (empty) database file and presents me with the brilliant new file MyData.edmx.sql.

Ok, so I assume that I need to execute this sql file against my new database file. But I cannot understand the magic to do this. When I right-click the sql file and select Execute, it tells me that I am using the Database Engine server type and it asks me for the server name. The only option available is MyPC \ SQLEXPRESS. But when I select this and click the Connect button, he complains that the [MyData] database does not exist (because I told her to use SQL Express and not LocalDb with my database file).

HELP. How to connect VS to a new database file?

Bean

+4
source share
2 answers

Got a good answer to my question from Allen Lee (Microsoft Tech support) on the MSDN forum [here] . It turns out that Visual Studio apparently cannot run SQL scripts directly against the database file. You need to download and install SQL Server Management Studio and use it to โ€œattachโ€ the file to an existing SQLEXPRESS instance.

Allen pointed me to a blog post that details the process:

http://www.headcrash.us/blog/2010/11/model-first-entity-framework-with-mdf-database-files-in-app_data/

0
source

I encountered a similar problem when trying to execute edmx.sql in VS 2013, EF6. This solution is not mentioned anywhere, so I will add it here for anyone who may encounter the error: "Mynewdatabase does not exist." Below is my solution:

  • Follow the instructions to compete with database generation from the model.
  • Open the * .edmx.sql file (it should have opened automatically). Right-click anywhere in the text editor and select Run.
  • If this is your first time connecting to this db, log in. A prompt will pop up. If you use LocalDB, enter the server in the same way as "(localdb) \ v11.0" and use Windows authentication.

Now the problem. If you get an error: "The database mynewdatabase does not exist", here's the fix: At the top of * .edmx.sql there is such a line:

USE [mynewdatabase]; 

You must change this to include the full path to the name of your database, for example. So:

 USE [D:\Projects\MyNewProject\MyNewProject\App_Data\mynewdatabase.mdf]; 

Now repeat step 2, and you should get a message that the database was created successfully.

Note1: .mdf extension required!

Note2: You must do this editing manually every time you create a database from a model, as this process overwrites edmx.sql.

+5
source

Source: https://habr.com/ru/post/1433456/


All Articles