Search code examples
mysqlmigrationasp.net-core-3.0.net-core-3.0pomelo-entityframeworkcore-mysql

.NET Core add migration ignoring some properties


I'm trying to run some migrations but it seems to ignore some properties.

Here is the Model that has the ignored property:

 public class UserRoleModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    internal long Id { get; set; }

    public long UserId { get; set; }

    public long RoleId { get; set; }

    [Column(TypeName = "bit")]
    public bool Active { get; set; }

    [Column(TypeName = "TIMESTAMP(6)")]
    public DateTime AddDate { get; set; }

    [Column(TypeName = "TIMESTAMP(6)")]
    public DateTime? DeleteDate { get; set; }

    public RoleModel Role { get; set; }
    public UserModel User { get; set; }

}

The DeleteDate property gets ignored. My UserModel has the same property with the same annotation but it gets added just fine.

This is what the migration builder creates:

         migrationBuilder.CreateTable(
            name: "UsersRoles",
            columns: table => new
            {
                Id = table.Column<long>(nullable: false)
                    .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                UserId = table.Column<long>(nullable: false),
                RoleId = table.Column<long>(nullable: false),
                Active = table.Column<ulong>(type: "bit", nullable: false, defaultValueSql: "true"),
                AddDate = table.Column<DateTime>(nullable: false, defaultValueSql: "CURRENT_TIMESTAMP(6)")
            },

The modelbuilder in the Context:

        modelBuilder.Entity<UserRoleModel>().ToTable("UsersRoles").HasKey(x => x.Id);
        modelBuilder.Entity<UserRoleModel>().Property(b => b.Active).HasDefaultValueSql("true");
        modelBuilder.Entity<UserRoleModel>().Property(b => b.AddDate).HasDefaultValueSql("CURRENT_TIMESTAMP(6)").ValueGeneratedOnAdd();

The only thing this model/entity has that others don't is that it includes foreign keys.

            modelBuilder.Entity<UserRoleModel>().HasOne(x => x.User).WithMany(y => y.UsersRoles).HasForeignKey(x => x.UserId);
        modelBuilder.Entity<UserRoleModel>().HasOne(x => x.Role).WithMany(y => y.UsersRoles).HasForeignKey(x => x.RoleId);

But that's the only difference. DbSet name for good measure:

        public DbSet<UserRoleModel> UsersRoles { get; set; }

I've deleted the migrations a few times over, but it keeps on adding the same exact code. Does anyone know what can create this behavior?


Solution

  • I am unable to reproduce this issue in Pomelo.EntityFrameworkCore.MySql version 3.0.1.

    I used the following code to test this, which works as expected:

    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    
    namespace IssueConsoleTemplate
    {
        public class UserRoleModel
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public long Id { get; set; }
    
            [Column(TypeName = "TIMESTAMP(6)")]
            public DateTime AddDate { get; set; }
    
            [Column(TypeName = "TIMESTAMP(6)")]
            public DateTime? DeleteDate { get; set; }
        }
    
        public class Context : DbContext
        {
            public DbSet<UserRoleModel> UsersRoles { get; set; }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder
                    .UseMySql("server=127.0.0.1;port=3306;user=root;password=;database=so59180050")
                    .UseLoggerFactory(LoggerFactory.Create(b => b
                        .AddConsole()
                        .AddFilter(level => level >= LogLevel.Information)))
                    .EnableSensitiveDataLogging()
                    .EnableDetailedErrors();
            }
        }
    
        internal class Program
        {
            private static void Main()
            {
            }
        }
    }
    

    Then a ran the following command:

    dotnet ef migrations add Initial --verbose
    

    It generated the following Migration:

    using System;
    using Microsoft.EntityFrameworkCore.Metadata;
    using Microsoft.EntityFrameworkCore.Migrations;
    
    namespace IssueConsoleTemplate.Migrations
    {
        public partial class Initial : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.CreateTable(
                    name: "UsersRoles",
                    columns: table => new
                    {
                        Id = table.Column<long>(nullable: false)
                            .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                        AddDate = table.Column<DateTime>(type: "TIMESTAMP(6)", nullable: false),
                        DeleteDate = table.Column<DateTime>(type: "TIMESTAMP(6)", nullable: true)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_UsersRoles", x => x.Id);
                    });
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.DropTable(
                    name: "UsersRoles");
            }
        }
    }