如何针对 MDF 文件运行 SQL 脚本?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,这对我有用。
当我选择“从模型生成数据库...”时,我创建了一个新的 MDF 文件。此过程运行良好,Visual Studio 生成了所需的 SQL 脚本。但是,我不知道如何连接到同一个 MDF 文件来运行脚本。
事实证明这很容易。
右键单击脚本并选择连接>连接
应该已经为本地 SQLEXPRESS 实例设置了服务器名称和身份验证。
单击选项按钮
单击其他连接参数选项卡
使用以下内容作为指导粘贴到数据库文件的路径:
AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;
单击连接按钮
如果连接仍然遇到问题,可能是因为已经有一个打开的连接。检查服务器资源管理器,如果连接已打开,请右键单击并选择关闭连接。
只要 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.
Right-click on the script and choose Connection > Connect
The server name and authentication should already be set for the local SQLEXPRESS instance.
Click the Options button
Click the Additional Connection Parameters tab
Paste in the the path to your database file using the following as a guide:
AttachDBFilename=C:\Path\To\Database\LocalData.mdf;database=LocalData;
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
我找到了一个解决方案,但它有点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.
我遇到了同样的问题,使用 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.
上面的答案很好,但我必须在 VS2013 中执行更多步骤,但我确实在此处的帮助下解决了,如下所示。
我希望这对某人有帮助 - 快乐编码!
Great Answers above but I had to do a few more steps in VS2013 but I did resolve with the help here as follows.
I hope this helps someone - Happy Coding!