1

Net MVC 3 web application. I generated my models from an existing database using "database first". But now, I would prefer to use the "code first" paradigm. Is that possible at all? How do I tell my solution that I want it to create tables for new models I code?

IamStalker
  • 3,813
  • 8
  • 50
  • 88
Jean-François Beauchamp
  • 5,027
  • 8
  • 37
  • 74
  • I don't checked if it's possible but maybe this link will help you: http://stackoverflow.com/questions/5446316/ef-4-1-code-first-vs-model-database-first – IamStalker Sep 02 '11 at 17:42
  • you are aware of the fact that all your tables will be dropped first in order to update the db to match the model in the current version of EF 4.1. There is no way of just creating new tables or updating current tables with Code First yet. – Major Byte Sep 02 '11 at 17:55
  • Another option is to tell the model's code generator to use the DbContext generator. That gives you classes to use that are closer to what code first gives you, and you only need to open the model if you change the database. As mentioned you can't update a schema with CF right now, for an existing database this is an easier way to go about it. – Tridus Sep 02 '11 at 18:04
  • If your main goal is to be able to use the Code First API rather than model first with database generation, you can use the Entity Framework Power Tools to continue with database first workflow. See http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d – tdykstra Sep 03 '11 at 01:41
  • @tdykstra My main goal was to be able to update the database structure on a remote development server for which I have an FTP access but no SQL access. The server does not accept remote connections to the SQL Server. But thanks for the link. Even if it is not what I was looking for, it is still interesting. – Jean-François Beauchamp Sep 04 '11 at 14:56

1 Answers1

2

Yes but be aware this happens if your database doesn't exist.

see http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx

  1. Add your context class that inherits from DbContext Declare your DbSet Customers {get;set;}
  2. Add your connect string to your sdf file.

Note the important part regarding your database creation: "This happens by default if your connection-string points to either a SQL CE or SQL Express database file that does not already exist on disk. You do not need to take any manual steps for this to happen."

So to add to an existing database this way - isn't going to happen.

Adam Tuliper - MSFT
  • 29,569
  • 4
  • 49
  • 68