EF4 Generate Database

丶灬走出姿态 提交于 2019-11-30 23:08:40

The method I've found to consistently work (at least with VS 2010 and SQL Server (2008|2008 R2) Express) is to:

  1. Create the ADO.NET Entity Model
  2. Create the .mdf database, but don't add any tables to it
  3. Copy the connection string from Server Explorer -> Modify Connection -> Advanced (bottom row)
  4. Close the connection in Server Explorer
  5. Generate the SQL script through "Generate Database from Model"
  6. When the SQL script is open in the editor, click "Connect", then go Options -> Additional Connection Parameters, and paste the connection string here. I also add ";Database=DATABASENAME;" to the end of the connection string.
  7. Execute SQL
  8. Disconnect and restart Visual Studio
  9. Now the tables should be available in Server Explorer after expanding the tree control for the .mdf file.

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, i.e. restarting Visual Studio, because otherwise Server Explorer isn't able to reopen the database.

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.

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.

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

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

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.

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.

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 AttachDbFilenamein the connection string?

Where is the "Additional Connection Parameter" tab? I only have and "Advanced" tab and it is not clear where you added the attach statement.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!