EF Code-first Learning Journey Database Initialization

Source: Internet
Author: User

1. createdatabaseifnotexists:

2.dropcreatedatabaseifmodelchanges:

3.Dropcreatedatabasealways:

4.Custom DB Initializer:

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base("schooldbconnectionstring") {Database.setinitializer<SchoolDBContext> (NewCreatedatabaseifnotexists<schooldbcontext>()); //database.setinitializer<schooldbcontext> (New dropcreatedatabaseifmodelchanges<schooldbcontext> (        )); //database.setinitializer<schooldbcontext> (New dropcreatedatabasealways<schooldbcontext> ()); //database.setinitializer<schooldbcontext> (New Schooldbinitializer ());    }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }} 
 Public class Schooldbinitializer:  createdatabaseifnotexists<schooldbcontext>{    protected  Overridevoid  Seed (schooldbcontext context)    {        base. Seed (context);}    }        

Initialization in configuration file

<?xml version="1.0" encoding="utf-8" ?>< configuration>    <appSettings>    <add key="Databaseinitializerfortype Schooldatalayer.schooldbcontext, Schooldatalayer"                 value="  System.Data.Entity.DropCreateDatabaseAlways ' 1[[schooldatalayer.schooldbcontext, Schooldatalayer]], EntityFramework" />    </appSettings></configuration>        

<?xml version="1.0" encoding="utf-8" ?>< configuration>    <appSettings>        <add key="Databaseinitializerfortype Schooldatalayer.schooldbcontext, Schooldatalayer"            value="  Schooldatalayer.schooldbinitializer, Schooldatalayer" />    </appsettings></ Configuration>

Turn off the database initialization feature

 Public classSchooldbcontext:dbcontext { PublicSchooldbcontext ():Base("schooldbconnectionstring")    {                    //Disable initializerDatabase.setinitializer<schooldbcontext> (NULL); }     PublicDbset<student> Students {Get;Set; }  PublicDbset<standard> Standards {Get;Set; }} 

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <appSettings>        <AddKey= "Databaseinitializerfortype schooldatalayer.schooldbcontext, Schooldatalayer"value= "Disabled" />    </appSettings></Configuration>

Initialize seed data

public class Schooldbinitializer:dropcreatedatabasealways<Schooldbcontext>{protected override void Seed (Schooldbcontext context) {IList< Standard>Defaultstandards = new List< Standard>();        Defaultstandards.add (New Standard () {standardname = ' standard 1 ', Description = "First Standard"});        Defaultstandards.add (New Standard () {standardname = ' standard 2 ', Description = ' Second standard '});        Defaultstandards.add (New Standard () {standardname = ' standard 3 ', Description = ' third Standard '}); foreach (Standard std in defaultstandards) context.        Standards.add (STD); Base.    Seed (context); }}        

EF Code-first Learning Journey Database Initialization

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: info-contact@alibabacloud.com 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.