简体   繁体   English

EF4生成数据库

[英]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。

Tools 工具

  • Visual Studio 2010 Visual Studio 2010
  • SQL Server 2008 Express SQL Server 2008 Express

Process 处理

  1. Create a new class project 创建一个新的类项目
  2. Add a new Server-Database item (mdf) named "Database1.mdf" to the project 将名为“Database1.mdf”的新服务器数据库项(mdf)添加到项目中
  3. Add an empty ADO.net Entity Model 添加一个空的ADO.net实体模型
  4. Create a simple entity (Person: Id, Name) 创建一个简单的实体(Person:Id,Name)
  5. Generate the Script selecting the Database1 connection created for me by visual studio 生成脚本,选择visual studio为我创建的Database1连接
  6. Right click the script editor and select the "Execute SQL..." option 右键单击脚本编辑器,然后选择“执行SQL ...”选项
  7. Log in to SQLEXPRESS 登录SQLEXPRESS

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的最干净的方法。

Ideal Solutions (in order of most prefered) 理想解决方案(按照最受欢迎的顺序)

  1. Get visual studio to attach the newly created database 获取visual studio以附加新创建的数据库
  2. Modify the generated SQL to point to file 修改生成的SQL以指向文件

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)是:

  1. Create the ADO.NET Entity Model 创建ADO.NET实体模型
  2. Create the .mdf database, but don't add any tables to it 创建.mdf数据库,但不要向其添加任何表
  3. Copy the connection string from Server Explorer -> Modify Connection -> Advanced (bottom row) 从服务器资源管理器 - >修改连接 - >高级(底行)复制连接字符串
  4. Close the connection in Server Explorer 在Server Explorer中关闭连接
  5. Generate the SQL script through "Generate Database from Model" 通过“从模型生成数据库”生成SQL脚本
  6. When the SQL script is open in the editor, click "Connect", then go Options -> Additional Connection Parameters, and paste the connection string here. 在编辑器中打开SQL脚本后,单击“连接”,然后转到“选项” - >“其他连接参数”,并在此处粘贴连接字符串。 I also add " ;Database=DATABASENAME; " to the end of the connection string. 我还在连接字符串的末尾添加“ ;Database=DATABASENAME; ”。
  7. Execute SQL 执行SQL
  8. Disconnect and restart Visual Studio 断开并重新启动Visual Studio
  9. Now the tables should be available in Server Explorer after expanding the tree control for the .mdf file. 现在,在扩展.mdf文件的树控件之后,这些表应该在Server Explorer中可用。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM