.NET Core 添加忽略某些属性的迁移

.NET Core add migration ignoring some properties

我正在尝试 运行 一些迁移,但它似乎忽略了一些属性。

这是被忽略的模型 属性:

 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; }

}

DeleteDate 属性 被忽略。我的 UserModel 具有相同的 属性 和相同的注释,但它添加得很好。

这是迁移构建器创建的内容:

         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)")
            },

上下文中的模型构建者:

        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();

这个 model/entity 唯一没有的是它包含外键。

            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);

但这是唯一的区别。 良好度量的 DbSet 名称:

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

我已经删除了几次迁移,但它一直在添加完全相同的代码。有谁知道什么可以造成这种行为?

我无法在 Pomelo.EntityFrameworkCore.MySql 版本 3.0.1 中重现此问题。

我使用以下代码对此进行了测试,它按预期工作:

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()
        {
        }
    }
}

然后运行下面的命令:

dotnet ef migrations add Initial --verbose

它生成了以下迁移:

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");
        }
    }
}