EF4 Generate Database
Posted
by shaneseaton
on Stack Overflow
See other posts from Stack Overflow
or by shaneseaton
Published on 2010-05-06T01:55:52Z
Indexed on
2010/05/06
1:58 UTC
Read the original article
Hit count: 526
Hi,
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.
Tools
- Visual Studio 2010
- SQL Server 2008 Express
Process
- Create a new class project
- Add a new Server-Database item (mdf) named "Database1.mdf" to the project
- Add an empty ADO.net Entity Model
- Create a simple entity (Person: Id, Name)
- Generate the Script selecting the Database1 connection created for me by visual studio
- Right click the script editor and select the "Execute SQL..." option
- Log in to SQLEXPRESS
This is where is falls over saying it cant find a database name "Database1".
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?
Is there a way that I can modify the TSQL script to use an un-attached database. 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.
Ideal Solutions (in order of most prefered)
- Get visual studio to attach the newly created database
- Modify the generated SQL to point to file
Thanks in advance.
© Stack Overflow or respective owner