开发者

ASP.Net MVC 3 with EF 4.1: Is it possible to switch from database first to code first

开发者 https://www.devze.com 2023-04-01 21:04 出处:网络
Net MVC 3 web application. I generated my models from an existing database using \"databas开发者_JS百科e first\". But now, I would prefer to use the \"code first\" paradigm. Is that possible at all? H

Net MVC 3 web application. I generated my models from an existing database using "databas开发者_JS百科e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消