Entity Framework comes with three approaches for development.
1) Database First: In this approach you already have your database and the entity framework will generate the model for you.
2) Model First: You don't have the database in this approach and you manually create the domain model using the entity framework designer. After creating the model the designer will generate the ddl statement which will be used to create the database.
3) Code First which I am covering in this post was introduced in EF 4.1. Code first allows you to create the domain model in the code without using the designer. After that EF will be generating the database for you. This approach is used only if you don't have the database the first time. If the database exists use the first approach. So here how the things goes.
The above class has no value on it's own . In order to take part in the database generation you have to create the context class.
Then the controller class.
Now the thing is that you have to run the application for the first time i.e query the database as done in the Index ActionMethod to generate the database. There are two possibilities here if it finds the connection string with the name UserContext in the web.config it will generate the database there.
If not then it will generate the database in the sqlexpress installed i.e go with convention.
After generating the database in the first try, if the model changes and you run it the second time it will generate an error. So to fix it you have to specify the strategy for EF that whenever it sees the database with the model changed then drop and recreate the database.
1) Database First: In this approach you already have your database and the entity framework will generate the model for you.
2) Model First: You don't have the database in this approach and you manually create the domain model using the entity framework designer. After creating the model the designer will generate the ddl statement which will be used to create the database.
3) Code First which I am covering in this post was introduced in EF 4.1. Code first allows you to create the domain model in the code without using the designer. After that EF will be generating the database for you. This approach is used only if you don't have the database the first time. If the database exists use the first approach. So here how the things goes.
public class User { [Key] public int UserID { get; set; } [Required] public string UserName { get; set; } [Required] public string Password { get; set; } [MaxLength(30)] public string EmailAddress { get; set; } }
The above class has no value on it's own . In order to take part in the database generation you have to create the context class.
public class UserContext: DbContext { public DbSet<User> Users { get; set; } }
Then the controller class.
public ViewResult Index() { return View(db.Users.ToList()); }
Running the application the first time.
Now the thing is that you have to run the application for the first time i.e query the database as done in the Index ActionMethod to generate the database. There are two possibilities here if it finds the connection string with the name UserContext in the web.config it will generate the database there.
<add name="UserContext" connectionString="Data Source=localhost;Initial Catalog=testing2;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
If not then it will generate the database in the sqlexpress installed i.e go with convention.
Running the application the second time.
After generating the database in the first try, if the model changes and you run it the second time it will generate an error. So to fix it you have to specify the strategy for EF that whenever it sees the database with the model changed then drop and recreate the database.
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); Database.SetInitializer(new DropCreateDatabaseIfModelChanges<UserContext>()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
Comments
Post a Comment