EF Codefirst, migrating multiple databases with a project

Source: Internet
Author: User

EF application Codefirst mode, basic usage points of data migration notes

EF6 Code First migration for multiple models

  1. Get ready

    First install the Entity Framework,

    Install-package EntityFramework

    Then create two of our own context,

    MyFirstContext.cs

    Using System.data.entity;namespace question.entityframework{public    class Myfirstcontext:dbcontext    {        Public Myfirstcontext (): Base ("Myfirst")        {        }}    }

    MySecondContext.cs

    Using System.data.entity;namespace question.entityframework{public    class Mysecondcontext:dbcontext    {        Public Mysecondcontext (): Base ("Mysecond")        {        }}    }
  2. Add Migrations Folder
    Enable-migrations-contexttypename question.entityframework.myfirstcontext-migrationsdirectory FirstMigrations
    Enable-migrations-contexttypename question.entityframework.mysecondcontext-migrationsdirectory SecondMigrations
    ContextTypeName: Databases (classes) that need to be migrated
    Migrationsdirectory: Specifies the directory where the files are migrated
  3. Create a migration record

    Create 2 entity classes, MyFirstEntity.cs and MySecondEntity.cs

    Then continue to enter in Package Manager console:

    Add-migration Initial-configurationtypename Question.entityframework.firstmigrations.configurationadd-migration Initial-configurationtypename Question.EntityFramework.SecondMigrations.Configuration
    -configurationtypename: Specifies the migration configuration for  different namespaces under different folders that are used by the migration configuration
  4. Generating data tables

Finally, configure the Web. config input:

Update-database-configurationtypename Question.EntityFramework.FirstMigrations.Configurationupdate- Database-configurationtypename Question.EntityFramework.SecondMigrations.Configuration

Done

EF Codefirst, migrating multiple databases with a project

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.