Set up entityframework to update the database automatically at development time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.