EF Core 代码首先自行创建附加属性
EF Core code first creates additional attribute on its own
我先在随机示例上练习代码,这是我的代码:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int SectionId { get; set; }
public string SpecializationName { get; set; }
public virtual Section Section { get; set; }
public virtual Specialization Specialization { get; set; }
}
public class Specialization
{
public string SpecializationName { get; set; }
}
public class EmployeeConfiguration : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable("Employees");
builder.Property(x => x.Name).IsRequired().HasMaxLength(100);
builder.HasOne(x => x.Section);
builder.HasOne(x => x.Specialization);
}
}
public class SpecializationConfiguration : IEntityTypeConfiguration<Specialization>
{
public void Configure(EntityTypeBuilder<Specialization> builder)
{
builder.ToTable("Specializations");
builder.HasKey(x => x.SpecializationName);
builder.Property(x => x.SpecializationName).IsRequired().HasMaxLength(20);
}
}
一切正常,除了在更新迁移后出现一个新属性 - SpecializationName1
in Employees
table。加一个是FK,原来不是,更奇怪。我完全不知道如何修复它,因为它几乎是自己做的,我从来没有在代码中输入 SpecializationName1
。
有趣,您似乎遇到了 EF Core 3.x 错误。
Employee
中的 属性 名称 SpecializationName
似乎不符合任何 EF Core FK 名称约定
If the dependent entity contains a property with a name matching one of these patterns then it will be configured as the foreign key:
<navigation property name><principal key property name>
<navigation property name>Id
<principal entity name><principal key property name>
<principal entity name>Id
所以它被丢弃了,但是不知何故默认生成的 FK 是相同的,并且由于它是 "reserved",EF 在末尾附加后缀“1”。
像往常一样,当 EF Core 约定不起作用时,解决方案是使用显式映射,例如在 EmployeeConfiguration
builder.HasOne(x => x.Specialization)
.WithMany()
.HasForeignKey(x => x.SpecializationName);
我先在随机示例上练习代码,这是我的代码:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int SectionId { get; set; }
public string SpecializationName { get; set; }
public virtual Section Section { get; set; }
public virtual Specialization Specialization { get; set; }
}
public class Specialization
{
public string SpecializationName { get; set; }
}
public class EmployeeConfiguration : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.ToTable("Employees");
builder.Property(x => x.Name).IsRequired().HasMaxLength(100);
builder.HasOne(x => x.Section);
builder.HasOne(x => x.Specialization);
}
}
public class SpecializationConfiguration : IEntityTypeConfiguration<Specialization>
{
public void Configure(EntityTypeBuilder<Specialization> builder)
{
builder.ToTable("Specializations");
builder.HasKey(x => x.SpecializationName);
builder.Property(x => x.SpecializationName).IsRequired().HasMaxLength(20);
}
}
一切正常,除了在更新迁移后出现一个新属性 - SpecializationName1
in Employees
table。加一个是FK,原来不是,更奇怪。我完全不知道如何修复它,因为它几乎是自己做的,我从来没有在代码中输入 SpecializationName1
。
有趣,您似乎遇到了 EF Core 3.x 错误。
Employee
中的 属性 名称 SpecializationName
似乎不符合任何 EF Core FK 名称约定
If the dependent entity contains a property with a name matching one of these patterns then it will be configured as the foreign key:
<navigation property name><principal key property name>
<navigation property name>Id
<principal entity name><principal key property name>
<principal entity name>Id
所以它被丢弃了,但是不知何故默认生成的 FK 是相同的,并且由于它是 "reserved",EF 在末尾附加后缀“1”。
像往常一样,当 EF Core 约定不起作用时,解决方案是使用显式映射,例如在 EmployeeConfiguration
builder.HasOne(x => x.Specialization)
.WithMany()
.HasForeignKey(x => x.SpecializationName);