gpt4 book ai didi

mysql - .NET Core 添加迁移忽略一些属性

转载 作者:行者123 更新时间:2023-11-29 15:23:53 25 4
gpt4 key购买 nike

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

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

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

关于mysql - .NET Core 添加迁移忽略一些属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com