EF4生成数据库 [英] EF4 Generate Database

查看:165
本文介绍了EF4生成数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我最难找到的第一模式创建一个基本的实体框架,比如最简单的方法。不过我挣扎与实际产生的数据库,对数据库的SQL的特别的运行。

I am trying my hardest to find the simplest way to create a basic "model first" entity framework example. However I am struggling with the actually generation of the database, particularly the running of the SQL against the database.

  • 在Visual Studio 2010中
  • 在SQL Server 2008中前preSS
  1. 创建一个新的类项目
  2. 在一个新的服务器,数据库项目(MDF)命名为Database1.mdf添加到项目
  3. 添加一个空ADO.net实体模型
  4. 创建一个简单的实体(人:身份证,姓名)
  5. 生成脚本选择由Visual Studio为我创建的数据库1连接
  6. 右键单击脚本编辑器,然后选择执行SQL ...选项
  7. 登录SQLEX $ P $干燥综合征

这是哪里倒下说它不能找到一个数据库名称数据库1。

This is where is falls over saying it cant find a database name "Database1".

问题是SQL服务器没有数据库1连接到它。我100%肯定的是Visual Studio中使用到数据库附加到SQLEx preSS时,它创建一个新的数据库(步骤2)。这似乎没有任何更多(VS2010甚至公测做到了)的情况。有人可以证实这一点?或者告诉我怎么去做到这一点?

The "problem" is that the SQL server has not had Database1 attached to it. I am 100% positive that Visual Studio use to attach a database to SQLExpress when it created a new database (Step 2). This appears to not be the case any more (even the beta of VS2010 did it). Can someone confirm this? or tell me how to get this to happen?

有没有办法,我可以修改TSQL脚本使用一个未连接数据库的方式。即一个文件。

Is there a way that I can modify the TSQL script to use an un-attached database. ie a file.

我知道我可以使用SQL Management Studio或SQLCMD附加数据库,但我的理想,以避免解决方案,因为我想看到的只是使用Visual Studio中的清洁方法。

I know I can use SQL Management Studio or sqlcmd to attach the database, but I would ideally like to avoid the solutions as I would like to see the cleanest method of just using visual studio.

  1. 获取Visual Studio中附上新创建的数据库
  2. 修改生成的SQL指向文件

在此先感谢。

推荐答案

感谢HighTechRider,为DataConnection在服务器资源管理器中确实有AttachDBFilename参数创建的连接字符串。

Thanks HighTechRider, the connection string created for the DataConnection in the server explorer did indeed have the "AttachDBFilename" parameter.

不过这个想法让我去尝试别的东西。

But this idea let me to try something else.

随着上面列出我到sqlex preSS的步骤做了一件在日志不同的相同的步骤。这一次,在连接到数据库引擎屏幕上,我点击显示更多选项按钮,然后就到了其他连接参数选项卡。

Following the same steps listed above I did something different at the log in to sqlexpress step. This time, in the Connect to Database Engine screen, I clicked the show more options button and went to the "Additional Connection Parameter" tab.

在这里我补充 AttachDBFilename = C:\ SRC \ mydatabasehome \ Database1.mdf;数据库=数据库1; 。请注意我用的完整路径,而不是 AttachDbFilename = | DataDirectory目录| \ Database1.mdf

In here i added AttachDBFilename=c:\src\mydatabasehome\Database1.mdf;database=Database1;. Note I used the full path, not AttachDbFilename=|DataDirectory|\Database1.mdf

这则执行我对数据库的SQL不会出现问题。

This then executed my sql against the database without an issue.

然后我又回到了服务器资源管理器,去扩大我的数据库连接,所以我可以看到我的新表,但失败的连接。经过一番放屁周围,我曾指出,无论出于何种原因,当我连接到SQLEx preSS情况下使用连接到数据库引擎屏幕上我在AttachDBFilename指定的数据库文件实际上是由一个永久连接。因此,previous连接字符串(使用AttachDBFilename参数)是因为该数据库已连接不再有效。

Then I went back to the server explorer and went to expand my database connection it so I could see my new tables, however the connection failed. After a bit of farting around, I worked out that for whatever reason, when I connected to the SQLExpress instance using the "Connect to Database Engine" screen the database file I specified in the AttachDBFilename actually made a permanent connection. Thus the previous connection string (using the AttachDBFilename parameter) was no longer valid as this database is already attached.

所以我删除连接到了一个数据库,它名为数据库1对我来说,选择SQLServer的创建一个新的连接直接。

So I deleted that connection and created a new connection directly to the SQLServer which had a database in it named "database1" for me to select.

现在我觉得它有所有制定了确定。

Now I think it has all worked out ok.

这是是一个奇怪的过程,当然意想不到的我,所以如果有人想解释一下是怎么回事,我一定会AP preciate它。现在我有一个办法(即使有点怪)做我想要的,更好的解决方案AP preciated。

This is was a weird process, and certainly unexpected for me, so if anyone would like to explain what is going on I would certainly appreciate it. For now I have a way (even if a bit weird) of doing what I want, better solutions appreciated.

希望这可以帮助别人。

这篇关于EF4生成数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆