.net core 一对一的表 建立
分别在两张表内建立导航属性和关联ID
//ResumeEntity
public class ResumeEntity: AuditedAggregateRoot<Guid>
{
public string Education
{
get; set;
}
public string StaffId
{
get; set;
}
public StaffEntity Staff
{
get; set;
}
}
//StaffEntity
public class StaffEntity: AuditedAggregateRoot<Guid>
{
public string Name
{
get; set;
}
public string Age
{
get; set;
}
public string ResumeId
{
get; set;
}
public ResumeEntity Resume
{
get; set;
}
}
需要把其中的一个表做为主体,加上外键
// 在使用HasForeignKey时需要指定哪张表有外键
builder.Entity<ResumeEntity>(b =>
{
b.ToTable("Resume")
.HasOne(x => x.Staff)
.WithOne(x => x.Resume)
.HasForeignKey<ResumeEntity>(x => x.StaffId);
});
一对一的关系如果不指定主体或者不指定外键的话,在生成Migration 时会发生异常
builder.Entity<ResumeEntity>(b =>
{
b.ToTable("Resume")
.HasOne()
});