1. NuGet download EntityFramework.
2. Define the context and open the NuGet command to execute enable-migrations, Libaray.DAL.Migrations.Configuration to be generated automatically after executing the command.
usingLibaray.Models.Entities;usingLibaray.Models.Maps;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacelibaray.dal{ Public classLibaraycontext:dbcontext { PublicLibaraycontext ():Base("name = Libcon") {Database.setinitializer (NewMigratedatabasetolatestversion<libaraycontext, libaray.dal.migrations.configuration>()); IMPORTANT} PublicDbset<usermodel> Usermodels {Get;Set; } PublicDbset<userinrolemodel> Userinrolemodels {Get;Set; } PublicDbset<userrolemodel> Userrolemodels {Get;Set; } PublicDbset<bookmodel> Bookmodels {Get;Set; } protected Override voidonmodelcreating (Dbmodelbuilder modelBuilder) {Base. Onmodelcreating (ModelBuilder); MODELBUILDER.CONFIGURATIONS.ADD (NewUserinrolemodelmap ()); MODELBUILDER.CONFIGURATIONS.ADD (NewUserrolemodelmap ()); MODELBUILDER.CONFIGURATIONS.ADD (NewUsermodelmap ()); MODELBUILDER.CONFIGURATIONS.ADD (NewBookmodelmap ()); } }}
3. Modify the Configuration
namespacelibaray.dal.migrations{usingSystem; usingSystem.Data.Entity; usingSystem.Data.Entity.Migrations; usingSystem.Linq; Internal Sealed classConfiguration:dbmigrationsconfiguration<libaray.dal.libaraycontext> { PublicConfiguration () {automaticmigrationsenabled=true; Automaticmigrationdatalossallowed=true; } protected Override voidSeed (Libaray.DAL.LibarayContext context) {}}}
4. Examples of entity classes
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacelibaray.models.entities{ Public classBookmodel { PublicGuid BookId {Get;Set; } Public stringBookName {Get;Set; } Public stringAuthor {Get;Set; } Public stringbookdescription {Get;Set; } PublicDateTime Bookdate {Get;Set; } PublicDateTime CreatedOn {Get;Set; } PublicGuid CreatedBy {Get;Set; } PublicDateTime Updatedon {Get;Set; } PublicGuid Updatedby {Get;Set; } }}
5. Entity class Mapping
usingLibaray.Models.Entities;usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity.ModelConfiguration;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacelibaray.models.maps{ Public classBookmodelmap:entitytypeconfiguration<bookmodel> { PublicBookmodelmap () { This. Haskey (U =u.bookid); This. Property (U =u.bookid). Hasdatabasegeneratedoption (System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); } }}
Set EntityFramework to update the database automatically at development time