【问题标题】:Entity Famework creates columns from objects实体框架从对象创建列
【发布时间】:2021-06-24 04:22:47
【问题描述】:

我在尝试在用户之间建立关系时遇到了问题。一个用户可以成为另一个用户的营养师,所以我有这样的结构:

public class ApplicationUser : IdentityUser
{
        [ForeignKey("MenteeId")]
        public virtual ICollection<MenteesDieticians> Dieticians { get; set; }

        [ForeignKey("DieticianId")]
        public virtual ICollection<MenteesDieticians> DieticianMentees { get; set; }
}

public class MenteesDieticians
{
        [Key]
        public int MenteesDieticiansId { get; set; }

        [Required]
        [ForeignKey("Mentee")]
        public string MenteeId { get; set; }

        [ForeignKey("MenteeId")]
        public virtual ApplicationUser Mentee { get; set; }

        [Required]
        [ForeignKey("Dietician")]
        public string DieticianId { get; set; }

        [ForeignKey("DieticianId")]
        public virtual ApplicationUser Dietician { get; set; }
}

我也在我的 DbContext 类中定义关系:

protected override void OnModelCreating(ModelBuilder builder)
{
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>()
            .HasMany(x => x.DieticianMentees)
            .WithOne()
            .HasForeignKey(x => x.DieticianId);
        builder.Entity<ApplicationUser>()
            .HasMany(x => x.Dieticians)
            .WithOne()
            .HasForeignKey(x => x.MenteeId);

        builder.Entity<MenteesDieticians>()
            .HasOne(x => x.Mentee)
            .WithOne()
            .HasForeignKey<MenteesDieticians>(t => t.MenteeId);
        builder.Entity<MenteesDieticians>()
            .HasOne(x => x.Dietician)
            .WithOne()
            .HasForeignKey<MenteesDieticians>(t => t.DieticianId);
}

最后,我的迁移代码如下所示:

    migrationBuilder.CreateTable(
        name: "MenteesDieticians",
        columns: table => new
        {
            MenteesDieticiansId = table.Column<int>(nullable: false)
                .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
            MenteeId = table.Column<string>(nullable: false),
            DieticianId = table.Column<string>(nullable: false),
            ApplicationUserId = table.Column<string>(nullable: true),
            ApplicationUserId1 = table.Column<string>(nullable: true)
        },

为什么我有那些 ApplicationUserId 和 ApplicationUserId1 列?如何解决?

您如何看待这样定义这些关系?老实说,我需要用户只有一个营养师,但我没有找到实现它的方法。

【问题讨论】:

  • 我将首先进入您的数据库并修复问题。然后刷新应该解决问题的paaping。我怀疑您在数据库中有一些键,当创建映射时,映射不知道如何解析重复的键名。

标签: c# entity-framework .net-core entity-framework-migrations


【解决方案1】:

尝试以下配置。首先,仅从一侧配置就足够了。其次,从模型中删除注释。

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<ApplicationUser>()
        .HasMany(x => x.Dieticians)
        .WithOne(x => x.Dietician)
        .HasForeignKey(x => x.DieticianId);
    builder.Entity<ApplicationUser>()
        .HasMany(x => x.DieticianMentees)
        .WithOne(x => x.Mentee)
        .HasForeignKey(x => x.MenteeId);
}

我不得不说,这里的设置有点奇怪。您能否详细说明您在这里的目标是什么?

【讨论】:

  • 哇它好用,谢谢,应用此配置后,我不再拥有那些额外的列。我的目标是在用户之间建立关系,让他们有另一个用户作为营养师。
  • 很好,只是要小心你映射到什么。在配置中,我将Dieticians 映射到DieticianID,并将DieticianMentees 映射到MenteeId。交叉检查这是否是您想要的。别忘了给答案投票:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多