1

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

这是具有被忽略属性的模型:

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

这个模型/实体唯一没有的东西是它包含外键。

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

我已经删除了几次迁移,但它继续添加相同的确切代码。有谁知道什么会造成这种行为?

4

1 回答 1

1

我无法在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");
        }
    }
}

于 2019-12-05T16:04:57.563 回答