0

So I have a ton of migration files like this that work and apply their changes when I start the application locally no problem whatsoever.

I also have defined my connection strings and database name and have quadruple checked it's right. But when I go to deploy the webapplication on the server the database isn't created, the migrations aren't applied. I figured that when the application was running on the server that the database would just be created by the application.

What am I missing?

Example migration file:

using Microsoft.EntityFrameworkCore.Migrations;

namespace Persistence.Migrations
{
   public partial class modificationstoprivsandroles : Migration
    {
       protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Name",
               table: "UserRoles");

            migrationBuilder.DropColumn(
                name: "Name",
                table: "Tags");

            migrationBuilder.DropColumn(
            name: "Name",
                table: "Privileges");

            migrationBuilder.AddColumn<int>(
                name: "Type",
                table: "UserRoles",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "Type",
                table: "Tags",
                nullable: false,
                defaultValue: 0);

            migrationBuilder.AddColumn<int>(
                name: "Type",
                table: "Privileges",
                nullable: false,
                defaultValue: 0);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Type",
                table: "UserRoles");

            migrationBuilder.DropColumn(
                name: "Type",
                table: "Tags");

            migrationBuilder.DropColumn(
               name: "Type",
                table: "Privileges");

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "UserRoles",
               type: "longtext CHARACTER SET utf8mb4",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "Tags",
                type: "longtext CHARACTER SET utf8mb4",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Name",
                table: "Privileges",
                type: "longtext CHARACTER SET utf8mb4",
                nullable: true);
        }
    }
}

Data Context

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //modelBuilder.Model.SetMaxIdentifierLength(30);
            ////Attempt 1:
            ////https://stackoverflow.com/questions/58821587/pomelo-entityframeworkcore-mysql-error-with-ef-core-3-0
            //modelBuilder.Entity<AppUser>(entity => entity.Property(m => m.Id).HasMaxLength(255));

            ////Other attempt.
            ////modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
            ////modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();
            ////https://stackoverflow.com/questions/20832546/entity-framework-with-mysql-and-migrations-failing-because-max-key-length-is-76

            modelBuilder.Entity<Value>().HasData(
                new Value { Id = 1, Name = "Value 1331" },
                new Value { Id = 2, Name = "Value 1332" },
                new Value { Id = 3, Name = "Value 1333" }
                );
...

Even have a Seed Data file.

namespace Persistence
{
    public class Seed
    {
        public static async Task SeedData(DataContext context,
            UserManager<AppUser> userManager)
        {
            if (!context.Privileges.Any())
            {
                var privileges = new List<Privilege>
                {
                    new Privilege
                    {
                        Id = Guid.Parse("11e36cdd-b1a0-489b-ad79-9e926eece529"),
                        Type = Privilege.PrivilegeTypes.ADMIN,
                        Description = "Administrator role"
                    }
                };

                await context.Privileges.AddRangeAsync(privileges);
                await context.SaveChangesAsync();
            }
...

Really not sure why the database isn't just kicking in here.

1
  • You said you already double checked connection string is correct. In that case I would add some logging to the text file (say, using Serilog) to see whether you are hitting the code you think you should be hitting. Also to log exceptions should they occur. Commented Sep 3, 2021 at 2:05

1 Answer 1

0

Can you try to execute your migrations inside Startup.cs -> Configure method

using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
      {
            var context = serviceScope.ServiceProvider.GetRequiredService<DataContext>();
            context.Database.Migrate();
      }

This should create the database from migrations. If you don't have creation codes inside your migration, you can try this:

  1. Delete your database from within SQL Server Object Explorer
  2. Delete all your existing migrations from within the Migrations folder.
  3. In Package-Management-Console type "Add-Migration InitialCreate" [optional, depending on your database initializer]
  4. In Package-Management-Console type "update-database"

adapted from: How to create database using code first migrations? and auto create database in Entity Framework Core

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.