带有InverseProperty的EF Core迁移问题

时间:2019-11-06 07:46:07

标签: c# entity-framework ef-core-3.0

我想在创建或更改记录时将用户ID作为外键保存在其他表中。为此,我使用了InverseProperty。创建迁移时,我有一个问题,即外键创建不正确。在数据库中创建太多字段。

我正在使用ef core 3.0。

public class User
{
    [Key]
    [Column("id")]
    public long Id { get; set; }

    [Column("usergroup_id")]
    public long UsergroupId { get; set; }
    public Usergroup Usergroup { get; set; } = null!;

    [Required]
    [Column("user_name")]
    public string? Username { get; set; }

    [Required]
    [Column("password")]
    public string? Password { get; set; }

    [Column("token")]
    public string? Token { get; set; }

    [Column("created_user_id")]
    public long? CreatedUserId { get; set; }
    public User? CreatedUser { get; set; }

    [Column("created")]
    public DateTime? Created { get; set; }

    [Column("updated_user_id")]
    public long? UpdatedUserId { get; set; }
    public User? UpdatedUser { get; set; }

    [Column("updated")]
    public DateTime? Updated { get; set; }

    [InverseProperty("CreatedUser")]
    public ICollection<User>? TblCreatedUser { get; } = null!;

    [InverseProperty("UpdatedUser")]
    public ICollection<User>? TblUpdatedUser { get; } = null!;
}

public class Usergroup
{
    [Key]
    [Column("id")]
    public long Id { get; set; }

    [Required]
    [Column("name")]
    public string? Name { get; set; }

    [Column("created_user_id")]
    public long? CreatedUserId { get; set; }
    public User? CreatedUser { get; set; } = null!;

    [Column("created")]
    public DateTime? Created { get; set; }

    [Column("updated_user_id")]
    public long? UpdatedUserId { get; set; }
    public User? UpdatedUser { get; set; } = null!;

    [Column("updated")]
    public DateTime? Updated { get; set; }

    [InverseProperty("Usergroup")]
    public ICollection<User> User { get; } = null!;
}

DBContext:

        modelBuilder.Entity<Usergroup>(entity =>
        {
            entity.HasKey(e => e.Id)
                .HasName("PK_tblUsergroup");

            entity.HasIndex(e => e.Name)
                .HasName("UQ_tblUsergroup_UsergroupName")
                .IsUnique();
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.HasKey(e => e.Id)
                .HasName("PK_tblUser");

            entity.HasIndex(e => e.Username)
                    .HasName("UQ_tblUser_Username")
                    .IsUnique();
        });

迁移:

tblUsergroup中的字段CreatedUserId1和UpdatedUserId1不应该存在。在其他表中是正确的。

migrationBuilder.CreateTable(
    name: "tblUsergroup",
    columns: table => new
    {
        id = table.Column<long>(nullable: false)
            .Annotation("SqlServer:Identity", "1, 1"),
        name = table.Column<string>(nullable: true),
        created_user_id = table.Column<long>(nullable: true),
        CreatedUserId1 = table.Column<long>(nullable: true),
        created = table.Column<DateTime>(nullable: true),
        updated_user_id = table.Column<long>(nullable: true),
        UpdatedUserId1 = table.Column<long>(nullable: true),
        updated = table.Column<DateTime>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_tblUsergroup", x => x.id);
    });

migrationBuilder.CreateTable(
    name: "tblUser",
    columns: table => new
    {
        id = table.Column<long>(nullable: false)
            .Annotation("SqlServer:Identity", "1, 1"),
        usergroup_id = table.Column<long>(nullable: false),
        user_name = table.Column<string>(nullable: true),
        password = table.Column<string>(nullable: false),
        token = table.Column<string>(nullable: true),
        created_user_id = table.Column<long>(nullable: true),
        created = table.Column<DateTime>(nullable: true),
        updated_user_id = table.Column<long>(nullable: true),
        updated = table.Column<DateTime>(nullable: true)
    },

1 个答案:

答案 0 :(得分:0)

尝试此代码

[Column("created_user_id")]
public long? CreatedUserId { get; set; }
[ForeignKey("CreatedUserId")]
public User? CreatedUser { get; set; } = null!;     
[Column("updated_user_id")]
public long? UpdatedUserId { get; set; }
[ForeignKey("UpdatedUserId")]
public User? UpdatedUser { get; set; } = null!;