The best tutorials for beginners
For Code First Approach:
For Database First Approach:
http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/setting-up-database
If you wish to use the default database of ASP.net MVC 5 identity and want to connect with SQL server, then do the following:
- make sure that your Entity Framework version in webconfig file matches the one installed in your project.
- If adding migration/updating database commands give any problem, uninstall the Entity Framework and Asp.NetIdentityEntityFramework (right click your project in solution explorer>>manage nuget packages>>installed)
clean solution and then exit visual studio
open the project again
now install the two packages (Entity Framework and Asp.NetIdentityEntityFramework) again.
build solution
in package manager console execute the following command:
Enable-migrations
if all goes well, update-database
- in web config file (in the root of the project), make changes in the entityframework tag, change the defaultConnectionFactory tag and the parameter value
and it should look like this
<entityFramework>
<defaultConnectionFactory type=”System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework”>
<parameters>
<parameter value=”Data Source=AJ; Integrated Security=True; MultipleActiveResultSets=True” />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName=”System.Data.SqlClient” type=”System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer” />
</providers>
</entityFramework>
make changes in the connection string and it should look like this:
<connectionStrings>
<add name=”CodeFirst4” connectionString=”Data Source=AJ;Initial Catalog=CodeFirst4;Integrated Security=True”
providerName=”System.Data.SqlClient” />
</connectionStrings>
[add name=CodeFirst4 is the name of the connection, AJ is the name of the server, Catalog=CodeFirst4 is the name of the database]
your ApplicationDbContext class must look like this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(): base(“CodeFirst4“)
{
}
[this CodeFirst4 must be the same name as that of the connection]
//THE PART BELOW IS OPTIONAL
in webconfig, in entity framework add contexts tag
<contexts>
<context type=”CodeFirst4.Models.ApplicationDbContext, CodeFirst4″>
<databaseInitializer type=”CodeFirst4.Models.SchoolInitializer, CodeFirst4″ />
</context>
</contexts>
in applicationdbcontext class add this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
}
Add Seed Data as told by ASP.net tutorials. (i created an intializer class in the models folder)
public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{