mysql - .NET Core 添加迁移忽略某些属性
问题描述
我正在尝试运行一些迁移,但它似乎忽略了一些属性。
这是具有被忽略属性的模型:
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; }
我已经删除了几次迁移,但它继续添加相同的确切代码。有谁知道什么会造成这种行为?
解决方案
我无法在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");
}
}
}
推荐阅读
- javascript - 如何从下面的代码片段加载 jquery?
- python - 如何在特定声音后提取音频?
- javascript - 获取特定 twilio 的电话和特定的出站电话 TWILIO 之间的 SMS 历史记录
- python - Twisted Server:如何跟踪服务器上的请求数量?
- angular - Angular:使用@ContentChildren 让孩子进入另一个组件
- mongodb - 如何使用 mongoDB 比较查询运算符检查 createdAt 日期之间的值?
- c++ - 在字符数组维度分析中构造字符数组
- ios - Alamore 将发布请求转换为服务器日志中的获取请求
- amazon-web-services - 我们可以使用预配置的 AMI 来创建 AWS EMR 集群吗?
- r - 如何根据列中的值扩展数据框