如何针对 MDF 文件运行 SQL 脚本?

发布于 2024-10-03 12:25:46 字数 190 浏览 0 评论 0原文

我使用 Entity Framework 4.0 使用模型优先方法创建了一个数据库模型。然后,我使用 从模型生成数据库... 创建了一个 sql 脚本,我还在 App_Data 文件夹中创建了一个 SQL Server 数据库文件。现在如何针对此 MDF 文件运行 SQL 文件?

我正在使用 Visual Studio 2010。

I've created a database model with model-first method using Entity Framework 4.0. I then created an sql script using the Generate Database from Model... I've also created an SQL Server Database file in my App_Data folder. How do I now run the SQL file against this MDF file?

I'm using Visual Studio 2010.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴情换悲伤 2024-10-10 12:25:46

我遇到了同样的问题,这对我有用。

当我选择“从模型生成数据库...”时,我创建了一个新的 MDF 文件。此过程运行良好,Visual Studio 生成了所需的 SQL 脚本。但是,我不知道如何连接到同一个 MDF 文件来运行脚本。

事实证明这很容易。

  1. 右键单击脚本并选择连接>连接

  2. 应该已经为本地 SQLEXPRESS 实例设置了服务器名称和身份验证。

  3. 单击选项按钮

  4. 单击其他连接参数选项卡

  5. 使用以下内容作为指导粘贴到数据库文件的路径:

    AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;

  6. 单击连接按钮

如果连接仍然遇到问题,可能是因为已经有一个打开的连接。检查服务器资源管理器,如果连接已打开,请右键单击并选择关闭连接。

只要 SQL 脚本保持连接,此过程还将创建持久连接。您需要关闭脚本或选择右键单击>连接>断开。

更多信息可以在这个问题中找到:
EF4 生成数据库

I ran into this same problem and here is what worked for me.

When I selected "Generate Database from Model..." I created a new MDF file. This process worked fine and Visual Studio generated the needed SQL Script. However, I did not know how to connect to the same MDF file to run the script.

It turned out to be quite easy.

  1. Right-click on the script and choose Connection > Connect

  2. The server name and authentication should already be set for the local SQLEXPRESS instance.

  3. Click the Options button

  4. Click the Additional Connection Parameters tab

  5. Paste in the the path to your database file using the following as a guide:

    AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;

  6. Click the Connect button

If you still have trouble connecting it may be because there is already an open connection. Check Server Explorer and if connection is open, Right-click and choose Close Connection.

This process will also create a persistent connection as long as the SQL Script remains connected. You will want to close the script or choose Right-click > Connection > Disconnect.

More information can be found at this question:
EF4 Generate Database

傲鸠 2024-10-10 12:25:46

我找到了一个解决方案,但它有点hacky。

我有 SQL Server Express (2008 R2)。因此,当从模型生成数据库时,我连接到它并让它在那里构建数据库。然后我转到C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA。 10_50 部分是因为版本不同,对您来说可能会有所不同。因此,在此文件夹中,有一个 .mdf 文件,其名称与数据库相同 - .mdf。还有一个 _log.ldf 文件。我将它们复制到我的项目的 App_Data 文件夹中。

这可行,但为模型中的每次更改生成新的 .mdf 数据库非常耗时。所以我在发货前这样做。

如果您找到更好的答案,请分享。

I found a solution, but it's a bit hacky.

I have SQL Server Express (2008 R2). So when generating the database from the model I connect to it and let it build a database there. Then I go to C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA. The 10_50 part is because of the version and can be different for you. So in this folder there's a .mdf file named just like the database - .mdf. There's also a _log.ldf file. I copied these to into the App_Data folder of my project.

This works but is very time consuming to generate a new .mdf database for every change in the model. So I do this just before shipping.

If you find a better answer please do share.

千柳 2024-10-10 12:25:46

我遇到了同样的问题,使用 EF5 在 mvc4 中使用模型优先方法。我所做的是创建项目、添加新模型、在 edmx 图上添加实体和关联。右键单击图表并选择“从模型生成数据库”。这会生成 sql 文件。

现在,在解决方案资源管理器的 App_Data 文件夹中添加一个 mdf 文件,例如 Database1.mdf(添加 > 新项目 > 数据 > SQL Server 数据库)。

右键单击>打开此 mdf 文件。现在在属性窗格中您有连接字符串。只需照原样复制即可。

恢复到生成的 SQL 脚本,右键单击 >执行。弹出窗口询问连接参数。确保服务器名称中的值正确(使用您的电脑名称或 127.0.0.1)。

现在,在选项中,单击“其他连接参数”选项卡并粘贴从 mdf 文件属性复制的连接字符串。

这对我来说非常有效并且非常合乎逻辑。无需直接使用 SQL Server 安装目录。

I encountered the same problem, Model First approach in mvc4 using EF5. What I did was create project, add a new model, add entities and associations on the edmx diagram. Right click on diagram and select 'Generate database from model'. This generates the sql file.

Now add a mdf file, say, Database1.mdf (Add > New item > Data > SQL Server Database) in the App_Data folder in solution explorer.

Right click > Open on this mdf file. Now in properties pane you have the connection string. Just copy that as it is.

Revert back to your generated SQL script, right click > Exeecute. A pop-up asks for connection parameters. Make sure you have correct value in Server name (use your PC name, or 127.0.0.1).

Now in options, Click the Additional Connection Parameters tab and paste the connection string copied from properties of mdf file.

This worked perfectly from me and is quite logical. There is no need to directly play around with the SQL Server installation directory.

日裸衫吸 2024-10-10 12:25:46

上面的答案很好,但我必须在 VS2013 中执行更多步骤,但我确实在此处的帮助下解决了,如下所示。

  1. 我从现有的 .mdf
  2. 右键单击​​ -> 创建了一个 ADO.NET 模型从模型生成数据库
  3. 右键单击​​解决方案资源管理器中的 .mdf 并打开
  4. 在属性窗格中将连接字符串复制到剪贴板
  5. 在 ADO 模型创建的 SQLQuery 窗口中单击更改连接
  6. 在新的“连接到服务器”窗口中 ->附加连接参数->从剪贴板粘贴连接字符串
  7. 在服务器资源管理器中右键单击数据库 ->新查询
  8. 将查询从 ADO 生成的脚本复制并粘贴到新查询窗口
  9. 将 USE [yourdatabase.mdf] 更改为 USE [longPath],其中 longPath 是顶部下拉列表中列出的非常长的路径。最简单的方法是开始输入并按 CTRL-SPACE 键。
  10. 运行查询
  11. 保存 longPath,因为您可以将其粘贴到 ADO 生成的任何新查询的 USE[] 中。

我希望这对某人有帮助 - 快乐编码!

Great Answers above but I had to do a few more steps in VS2013 but I did resolve with the help here as follows.

  1. I created a ADO.NET Model from existing .mdf
  2. Right Clicked -> Generate Database from model
  3. Right click the .mdf in Solution Explorer and Open
  4. In the properties pane copy Connection String to clipboard
  5. In the SQLQuery window created by ADO Model click change connection
  6. In the new Connect to Server window -> Additional Connection Parameters -> paste connection string from clipboard
  7. In Server Explorer right click the Database -> New Query
  8. Copy and Paste the Query from ADO Generated script to new Query Window
  9. Change USE [yourdatabase.mdf] to USE [longPath] where longPath is the very long path listed in the drop down at top. The easiest way is to start typing and CTRL-SPACE it.
  10. Run the Query
  11. Save the longPath because you can just paste it into the USE[] on any new Query generated by ADO.

I hope this helps someone - Happy Coding!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文