使用 EFCore 为一对一关系生成迁移时出错
Error while generating migration for One to One relation using EFCore
我有两个型号如下:
public class Person{
public virtual int Id { get; set; }
public virtual int BaseId { get; set; }
public virtual string Name { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee{
public virtual int Id { get; set; }
public virtual string Code{ get; set; }
public virtual Person Person { get; set; }
}
每个 Employee
都是一个 Person
,但每个 Person
不一定是一个 Employee
。这两者之间的关系是一对一关系类型,但我需要在非主键列(Person.BaseId
)和所需的外键列(Employee.Id
)之间建立这种关系。在面对Employee
模型中的Id
列同时是主键和外键列。
我有这个映射配置:
public override void Configure(EntityTypeBuilder<Person> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblPeople", "dbo");
builder
.HasOne(p => p.Employee)
.WithOne(p => p.Person)
.HasForeignKey<Employee>(p => p.Id)
.HasPrincipalKey<Person>(p => p.BaseId);
}
public override void Configure(EntityTypeBuilder<Employee> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblEmployees", "dbo");
}
当我尝试生成迁移时出现以下错误:
The child/dependent side could not be determined for the one-to-one
relationship between 'Employee.Person' and 'Person.Employee'. To
identify the child/dependent side of the relationship, configure the
foreign key property. If these navigations should not be part of the
same relationship configure them without specifying the inverse. See
http://go.microsoft.com/fwlink/?LinkId=724062 for more details.
我不想使用数据注释方法来解决这个问题。
我找到了解决方案,错误出在我的 EntityTypeConfiguration
classes 设计中。我有以下设计:
为简单起见,我裁剪了代码
public abstract class BaseMap<T, U> : IEntityTypeConfiguration<T> where T: BaseEntity<U>
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.HasKey(x => x.Id);
}
}
public abstract class ChildBaseMap<T, U> : BaseMap<T, U> where T: ChildBaseEntity<U>
{
public virtual void Configure(EntityTypeBuilder<T> builder)//<== virtual is wrong here as I need to override the parent Configure
{
base.Configure(builder);
builder.Property(x => x.BaseId).IsRequired();
}
}
public class PersonMap : ChildBaseMap<Person, int>
{
public override void Configure(EntityTypeBuilder<Person> builder)
{
....
只需override
Configure 方法,而不是在ChildBaseMap
中将其定义为virtual
class 解决问题![=15=]
我有两个型号如下:
public class Person{
public virtual int Id { get; set; }
public virtual int BaseId { get; set; }
public virtual string Name { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee{
public virtual int Id { get; set; }
public virtual string Code{ get; set; }
public virtual Person Person { get; set; }
}
每个 Employee
都是一个 Person
,但每个 Person
不一定是一个 Employee
。这两者之间的关系是一对一关系类型,但我需要在非主键列(Person.BaseId
)和所需的外键列(Employee.Id
)之间建立这种关系。在面对Employee
模型中的Id
列同时是主键和外键列。
我有这个映射配置:
public override void Configure(EntityTypeBuilder<Person> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblPeople", "dbo");
builder
.HasOne(p => p.Employee)
.WithOne(p => p.Person)
.HasForeignKey<Employee>(p => p.Id)
.HasPrincipalKey<Person>(p => p.BaseId);
}
public override void Configure(EntityTypeBuilder<Employee> builder)
{
builder.HasKey(x => x.Id);
builder.ToTable("tblEmployees", "dbo");
}
当我尝试生成迁移时出现以下错误:
The child/dependent side could not be determined for the one-to-one relationship between 'Employee.Person' and 'Person.Employee'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.
我不想使用数据注释方法来解决这个问题。
我找到了解决方案,错误出在我的 EntityTypeConfiguration
classes 设计中。我有以下设计:
为简单起见,我裁剪了代码
public abstract class BaseMap<T, U> : IEntityTypeConfiguration<T> where T: BaseEntity<U>
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.HasKey(x => x.Id);
}
}
public abstract class ChildBaseMap<T, U> : BaseMap<T, U> where T: ChildBaseEntity<U>
{
public virtual void Configure(EntityTypeBuilder<T> builder)//<== virtual is wrong here as I need to override the parent Configure
{
base.Configure(builder);
builder.Property(x => x.BaseId).IsRequired();
}
}
public class PersonMap : ChildBaseMap<Person, int>
{
public override void Configure(EntityTypeBuilder<Person> builder)
{
....
只需override
Configure 方法,而不是在ChildBaseMap
中将其定义为virtual
class 解决问题![=15=]