Entity Framework Code First steps:
Entity Framework Code First steps:
1. Create a Context class which is a subclass of DbContext
using System.Data.Entity;
namespace MovieStore.Models
{
public class MovieStoreContext: DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Movie> Movies { get; set; }
}
}
2. Add a connection string in the web.config file
<connectionStrings>
<add name="MovieStoreContext" connectionString="Data Source=HOUDEV206D\MSSQLSERVER08;Initial Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Imp: The connection string name and the Context class name must be the same. If no connection string is specified in the web.config file a new database file(.mdf) will be created under the App_Data folder.
3. Run the following commands in Package Manager Console:
a. enable-migrations
b. add-migration 'InitialModel'
c. update-database
4. The first three steps creates a new database. From now on whenever we make changes to the existing models(Customer/Movies) or add new models (after creating the new models we should add those to the MovieContextStore class) we should run the following commands in the Package Manager Console.
a. add-migration 'UpdateModel'
b. update-database
1. Create a Context class which is a subclass of DbContext
using System.Data.Entity;
namespace MovieStore.Models
{
public class MovieStoreContext: DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Movie> Movies { get; set; }
}
}
2. Add a connection string in the web.config file
<connectionStrings>
<add name="MovieStoreContext" connectionString="Data Source=HOUDEV206D\MSSQLSERVER08;Initial Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Imp: The connection string name and the Context class name must be the same. If no connection string is specified in the web.config file a new database file(.mdf) will be created under the App_Data folder.
3. Run the following commands in Package Manager Console:
a. enable-migrations
b. add-migration 'InitialModel'
c. update-database
4. The first three steps creates a new database. From now on whenever we make changes to the existing models(Customer/Movies) or add new models (after creating the new models we should add those to the MovieContextStore class) we should run the following commands in the Package Manager Console.
a. add-migration 'UpdateModel'
b. update-database
Comments
Post a Comment