[英]EF4 Generate Database
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.
但是,我正在努力实际生成数据库,尤其是针对数据库运行SQL。
This is where is falls over saying it cant find a database name "Database1". 这就是它无法找到数据库名称“Database1”的地方。
The "problem" is that the SQL server has not had Database1 attached to it. “问题”是SQL服务器没有附加Database1。 I am 100% positive that Visual Studio use to attach a database to SQLExpress when it created a new database (Step 2).
我100%肯定Visual Studio在创建新数据库时将数据库附加到SQLExpress(步骤2)。 This appears to not be the case any more (even the beta of VS2010 did it).
这似乎不再是这种情况(即使是VS2010的测试版也是如此)。 Can someone confirm this?
有人能证实吗? or tell me how to get this to happen?
或告诉我如何实现这一目标?
Is there a way that I can modify the TSQL script to use an un-attached database. 有没有办法可以修改TSQL脚本以使用未附加的数据库。 ie a file.
即一个文件。
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. 我知道我可以使用SQL Management Studio或sqlcmd来附加数据库,但我最好避免使用解决方案,因为我希望看到使用visual studio的最干净的方法。
Thanks in advance. 提前致谢。
The method I've found to consistently work (at least with VS 2010 and SQL Server (2008|2008 R2) Express) is to: 我发现一直工作的方法(至少使用VS 2010和SQL Server(2008 | 2008 R2)Express)是:
;Database=DATABASENAME;
" to the end of the connection string. ;Database=DATABASENAME;
”。 Probably not all of the steps are required (especially not in the exact order shown above), as I've not really optimized the process -- if it works, don't fix it. 可能并非所有步骤都是必需的(特别是不按照上面显示的确切顺序),因为我没有真正优化过程 - 如果它有效,请不要修复它。 :-) The one crucial step seems to be #8, ie restarting Visual Studio, because otherwise Server Explorer isn't able to reopen the database.
:-)一个关键步骤似乎是#8,即重新启动Visual Studio,因为否则Server Explorer无法重新打开数据库。
Thanks HighTechRider, the connection string created for the DataConnection in the server explorer did indeed have the "AttachDBFilename" parameter. 感谢HighTechRider,为服务器资源管理器中的DataConnection创建的连接字符串确实具有“AttachDBFilename”参数。
But this idea let me to try something else. 但是这个想法让我尝试别的东西。
Following the same steps listed above I did something different at the log in to sqlexpress step. 按照上面列出的相同步骤,我在登录sqlexpress步骤时做了一些不同的事情。 This time, in the
Connect to Database Engine
screen, I clicked the show more options button and went to the "Additional Connection Parameter" tab. 这一次,在“
Connect to Database Engine
屏幕中,我单击了“显示更多选项”按钮,然后转到“其他连接参数”选项卡。
In here i added AttachDBFilename=c:\\src\\mydatabasehome\\Database1.mdf;database=Database1;
在这里我添加了
AttachDBFilename=c:\\src\\mydatabasehome\\Database1.mdf;database=Database1;
. 。 Note I used the full path, not
AttachDbFilename=|DataDirectory|\\Database1.mdf
注意我使用了完整路径,而不是
AttachDbFilename=|DataDirectory|\\Database1.mdf
This then executed my sql against the database without an issue. 这样就可以在没有问题的情况下对数据库执行我的sql。
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.
经过一段时间的放屁之后,无论出于何种原因我都解决了这个问题,当我使用“连接到数据库引擎”屏幕连接到SQLExpress实例时,我在AttachDBFilename中指定的数据库文件实际上建立了永久连接。 Thus the previous connection string (using the AttachDBFilename parameter) was no longer valid as this database is already attached.
因此,先前的连接字符串(使用AttachDBFilename参数)不再有效,因为此数据库已附加。
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. 所以我删除了那个连接并直接创建了一个新连接到SQLServer,其中有一个名为“database1”的数据库供我选择。
Now I think it has all worked out ok. 现在我认为一切都很好。
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.
现在我有办法(即使有点奇怪)做我想要的,更好的解决方案赞赏。
Hope this helps someone. 希望这有助于某人。
What does your connection string look like? 你的连接字符串是什么样的? Have you tried putting an
AttachDbFilename
in the connection string? 您是否尝试将
AttachDbFilename
放在连接字符串中?
Where is the "Additional Connection Parameter" tab? “附加连接参数”选项卡在哪里? I only have and "Advanced" tab and it is not clear where you added the attach statement.
我只有“高级”选项卡,并且不清楚添加attach语句的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.