no pending explicit migrations

Jon Jacobs 86 Reputation points
2021-04-21T14:09:58.843+00:00

I am using VS2017 pro. I created an MVC5 solution in C# and opened Package Manager Console. In the Entities folder I created a class and marked it up with appropriate annotations. update-database command created the table (and some initial other tables). I created another class in the Entities folder, identical to the first with the same markup, except for the class name and table name.
When I ran update-database it reported "no pending explicit migrations" and did not create the new table.
Why did the first one work and the second one (just a minute after) fail? What do I do to make it create the new table? SQL Server Express, LocalDb. There should not be any problem with the database itself, since the first migration worked, and the second, that did not work, in the same session of the application and Package Manager.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,395 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,270 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Chao Deng-MSFT 796 Reputation points
    2021-04-22T03:20:12.213+00:00

    Hi @Jon Jacobs ,
    If you are using Code First for data migration, and you modify the Model after executing it once, you can follow the commands below.

    1.Enable-Migrations -EnableAutomaticMigrations –Force  
    

    The execution of Enable-Migrations may be interrupted due to an error. At this time, you need to run the parameter-added command -Force.

    2.Add-Migration Initial  
    

    The Add-Migration command scaffolds your changes into a cs file. This cs file is placed in the same folder as the configuration file for the DbContext you are targeting.

    3.Update-Database -Verbose  
    

    Then you use Update-Database to update the database. Specify the -Verbose flag so that you can see the SQL that Code First Migrations is running.
    You can refer to Code migration.


    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,

    ChaoDeng

    0 comments No comments

  2. Jon Jacobs 86 Reputation points
    2021-04-22T14:03:53.557+00:00

    Thank you, @Chao Deng-MSFT . That was very helpful. I ran the first two commands,
    Added a test class:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Web;

    namespace Memberships.Entities
    {
    [Table("Something")]
    public class Something
    {
    [MaxLength(80)]
    public string Title { get; set; }
    }
    }
    I then ran update-database -verbose. The result was strange, including a long hex value:
    PM> update-database -verbose
    Using StartUp project 'Memberships'.
    Using NuGet project 'Memberships'.
    Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
    Target database is: 'MembershipsDb' (DataSource: (LocalDb)\MSSQLLocalDB, Provider: System.Data.SqlClient, Origin: Configuration).
    Applying explicit migrations: [202104221349217_initial].
    Applying explicit migration: 202104221349217_initial.
    INSERT [dbo].__MigrationHistory
    VALUES (N'202104221349217_initial', N'Memberships.Migrations.Configuration', 0x1F8B0800000000000400DD5CDB6EE436127D5F60FF41D0D366E1B47CD919CC1AED044EDBCE1A3BBE60DA13ECDB... etc. , N'6.2.0-61023')

    Running Seed method.
    PM>