Usually after we have written the entity, we configure the data context object, and after the connection string is changed in the configuration file. It is not possible to build the database and generate the database automatically, there are two key steps: 1. Enable Migrations 2. Update-database after playing the second step, the database is automatically generated. ------------------------------------instance----------------------------------------------------------------// Entity: Using system;using system.collections.generic;using system.componentmodel.dataannotations;using System.Linq; Using System.Web; Namespace pagingandsortinginmvc.entities{Public class Employeemaster {[Key]Public string ID {get; set;} [Required (errormessage= "Please enter employee Name")]Public string Name {get; set;} [Required (errormessage= "Please enter mobile number")]Public string PhoneNumber {get; set;} [Required (errormessage= "Please enter email")]Public string Email {get; set;} [Required (errormessage= "Please enter Salary")]Public decimal Salary {get; set;}}} ------------------------------------------------------------------------------------------------------------- Created data context class: Using Pagingandsortinginmvc.entities;using system;using system.collections.generic;using System.data.entity;using system.linq;using system.web; Namespace pagingandsortinginmvc.models{Public class Applicationdbcontext:dbcontext {Public Applicationdbcontext (): Base ("name=connectionstring") { } Public dbset<employeemaster> Employees {get; set;}}}-----------------------------------------------------------------------------------------------------the database connection string in the configuration file:<connectionStrings><add name= "ConnectionString" connectionstring= "server=.; Database=pageandsortdb;uid=sa;pwd=password_1 "providername=" System.Data.SqlClient "/></connectionStrings> ----------------------------------------------------------------------------------------------------------- ----------------------------------------------- after setting these, compile the project: just run:1. Enable Migrations----------generate a class file "Configuration.cs", change automaticmigrationsenabled property to true namespace pagingandsortinginmvc.migrations{ using system; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; internal sealed class Configuration : dbmigrationsconfiguration<pagingandsortinginmvc.models.applicationdbcontext> { public configuration () { AutomaticMigrationsEnabled = true; } protected override void seed ( pagingandsortinginmvc.models.applicationdbcontext&Nbsp;context) { // this method will be called after migrating to the latest version. // you can use the dbset<t>. AddOrUpdate () helper extension method // to avoid creating duplicate seed data. e.g. // // context. People.addorupdate ( // p => p.fullname, &nbSp; // new person { fullname = "Andrew peters" }, // new Person { FullName = "brice Lambson " }, // new Person { FullName = "Rowan miller" } // ); // } }} 2. Update-database ---after playing this, we have built our database in the database, data sheet
MVC automatically generates database "Code-first mode"