C# 实体工作从对象创建列
当我试图在我的用户之间创建reations时,我面临一个问题。一个用户可以是另一个用户的营养师,因此我有以下结构:C# 实体工作从对象创建列,c#,entity-framework,.net-core,entity-framework-migrations,C#,Entity Framework,.net Core,Entity Framework Migrations,当我试图在我的用户之间创建reations时,我面临一个问题。一个用户可以是另一个用户的营养师,因此我有以下结构: public class ApplicationUser : IdentityUser { [ForeignKey("MenteeId")] public virtual ICollection<MenteesDieticians> Dieticians { get; set; } [ForeignK
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; }
}
公共类应用程序用户:IdentityUser
{
[外键(“门蒂ID”)]
公共虚拟ICollection营养师{get;set;}
[外键(“DieticianId”)]
公共虚拟ICollection营养师学员{get;set;}
}
公开课教师
{
[关键]
public int-MenteesDieticiansId{get;set;}
[必需]
[外籍人员(“学员”)]
公共字符串MenteeId{get;set;}
[外键(“门蒂ID”)]
公共虚拟应用程序用户成员{get;set;}
[必需]
[外籍人士(“营养师”)]
公共字符串DieticianId{get;set;}
[外键(“DieticianId”)]
公共虚拟应用程序用户营养师{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);
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasMany(x=>x.DieticianMentees)
.WithOne()
.HasForeignKey(x=>x.DieticianId);
builder.Entity()
.有很多(x=>x.营养师)
.WithOne()
.HasForeignKey(x=>x.MenteeId);
builder.Entity()
.HasOne(x=>x.Mentee)
.WithOne()
.HasForeignKey(t=>t.MenteeId);
builder.Entity()
.HasOne(x=>x.营养师)
.WithOne()
.HasForeignKey(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)
},
migrationBuilder.CreateTable(
姓名:“精神病学家”,
列:表=>new
{
MenteesDieticiansId=table.Column(可空:false)
.Annotation(“MySQL:ValueGenerationStrategy”,MySQLValueGenerationStrategy.IdentityColumn),
MenteeId=table.Column(可空:false),
DieticianId=表.列(可为空:false),
ApplicationSerid=table.Column(可空:true),
ApplicationUserId1=table.Column(可空:true)
},
为什么我有那些applicationSerid和applicationSerid1列?如何修复
您认为如何定义这样的关系?老实说,我需要用户只有一个营养师,但我没有找到实现它的方法。尝试以下配置。首先,仅从一侧配置就足够了。其次,从模型中删除注释
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.有很多(x=>x.营养师)
.有一个(x=>x.营养师)
.HasForeignKey(x=>x.DieticianId);
builder.Entity()
.HasMany(x=>x.DieticianMentees)
.带一个(x=>x.学员)
.HasForeignKey(x=>x.MenteeId);
}
我不得不说,您的设置有点奇怪。您能详细说明一下您的目标吗?我会先进入您的数据库并修复问题。然后刷新paaping以修复问题。我怀疑您的数据库中有一些密钥,在创建映射时,映射不知道如何解决重复的密钥names.哇,这很有效,谢谢你,在应用此配置后,我不再有那些额外的列。我的目标是在用户之间建立关系,允许他们有另一个用户作为营养师。太好了,只需小心映射到什么。在配置中,我将
营养师映射到营养师ID
,并且DieticianMentees
到MenteeId
。如果这是你想要的,交叉检查。别忘了投票给答案:)