How to do AutoMigration in Entityframework core without using command (Using Programming only)

JaySoni 1910 6 Reputation points
2021-03-18T12:43:17.46+00:00

I use EF core in My project and I want migrate my context using Programming (not command) So I use these two concpets one by one

1.

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())

{

    var context = serviceScope.ServiceProvider.GetService<UserInformationContext>();

    context.Database.Migrate();

}

2.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserInformationContext userContext)

{
   userContext.Database.Migrate();

}

Problem Is ,using this two concepts can not create any Migration Folder like add-migration command Is there any way to Create Migration Folder and Migrate Context using Programming ??

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
SQL Server Migration Assistant
SQL Server Migration Assistant
A Microsoft tool designed to automate database migration to SQL Server from Access, DB2, MySQL, Oracle, and SAP ASE.
532 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Wang-MSFT 1,061 Reputation points
    2021-03-19T05:47:53.237+00:00

    Hi, @JaySoni 1910

    From the official document of EF, we can see

    EF Core will include most of the features of EF 6 gradually. However, there are some features of EF 6 which are not supported in EF Core 2.0 such as:

    EDMX/ Graphical Visualization of Model
    Entity Data Model Wizard (for DB-First approach)
    ObjectContext API
    Querying using Entity SQL.
    Automated Migration
    Inheritance: Table per type (TPT)
    Inheritance: Table per concrete class (TPC)
    Many-to-Many without join entity
    Entity Splitting
    Spatial Data
    Lazy loading of related data
    Stored procedure mapping with DbContext for CUD operation
    Seed data
    Automatic migration

    userContext.Database.Migrate(); will update the database based on the migrations you have created. So you need to add migrations at first.

    ------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Michael Wang

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.