尽管配置了 FK,EF Core 仍创建影子属性
EF Core creating shadow properties despite FK being configured
我发现 EF 核心正在为我的外键生成影子属性,尽管我正在配置它们,从我从文档中可以看出,它应该只在它不能匹配列的数据类型时才这样做,但他们都是 Guid。
这是我 FK 的一部分:
public class Property : BaseEntity<Guid>
{
public Property(Guid Id, List<Classification> classifications) : this(Id)
{
_classifications = classifications;
}
public Property(Guid id)
{
Id = id;
_attributes = new List<PropertyAttribute>();
_classifications = new List<Classification>();
_images = new List<PropertyImage>();
_siteVistis = new List<PropertySiteVisitLink>();
}
private readonly List<Classification> _classifications;
public IEnumerable<Classification> Classifications => _classifications.AsReadOnly();
}
另一半:
public class Classification
{
public Classification(Guid id, Guid propertyId, int classificationBatchId)
{
_id = id;
PropertyId = propertyId;
ClassificationBatchId = classificationBatchId;
_classificationResults = new List<ClassificationResult>();
}
private Guid _id;
public Guid Id => _id;
public Guid PropertyId { get; private set; }
}
ef 核心的配置在这里:
{
public void Configure(EntityTypeBuilder<Property> builder)
{
builder.ToTable(ConfigConstants.PropertyTableName, ConfigConstants.VacantsSchema);
builder.HasKey(p => p.Id);
builder.Property(p => p.Id).HasColumnName("Id").ValueGeneratedNever();
builder.HasMany<Classification>("_classifications").WithOne().HasForeignKey(c => c.PropertyId);
builder.Navigation<Classification>(p => p.Classifications).UsePropertyAccessMode(PropertyAccessMode.Field).HasField("_classifications");
}
}
然而,当我进行迁移时,它会创建一个 propertyId1 列并将 FK 附加到该列以及 PropertyId 字段,知道为什么要创建这个新列吗?
edit
i think its something to do with the nullability(?) of the column and the FK, in the Classification object, PropertyId is a Guid and set to non nullable, the PropertyId1 one it always wants to create is a nullable Guid, but im not sure how to force it to be non nullable, or just use the property i tell it to?
EF Core creating shadow properties despite FK being configured
问题是这里
builder.HasMany<Classification>("_classifications") // <--
.WithOne()
.HasForeignKey(c => c.PropertyId);
FK 与由 _classifications
字段表示的 navigation 关联(根据 EF Core 模型术语,它是 navigation 属性 事件虽然按 CLR/C# 术语来说它是一个字段)。因此,由 public IEnumerable<Classification> Classifications
表示的关系未映射,EF Core 假定它是单独的关系并将其映射到常规影子 FK(和列)名称,在这种情况下具有数字后缀,因为原始常规名称已被保留其他已经明确映射的关系。
当然你不想要两个关系,所以把FK映射到真实的就可以了属性
builder.HasMany(e => e.Classifications) // collection navigation property
.WithOne() // no inverse reference navigation property
.HasForeignKey(c => c.PropertyId); // FK property
下一行只是将 属性 与支持字段相关联并设置 EF 使用的访问模式。由于这些是 EFC 3.0+ 的默认值,因此可以安全地跳过它(不需要,多余)
builder.Navigation(e => e.Classifications)
.UsePropertyAccessMode(PropertyAccessMode.Field)
.HasField("_classifications");
我发现 EF 核心正在为我的外键生成影子属性,尽管我正在配置它们,从我从文档中可以看出,它应该只在它不能匹配列的数据类型时才这样做,但他们都是 Guid。 这是我 FK 的一部分:
public class Property : BaseEntity<Guid>
{
public Property(Guid Id, List<Classification> classifications) : this(Id)
{
_classifications = classifications;
}
public Property(Guid id)
{
Id = id;
_attributes = new List<PropertyAttribute>();
_classifications = new List<Classification>();
_images = new List<PropertyImage>();
_siteVistis = new List<PropertySiteVisitLink>();
}
private readonly List<Classification> _classifications;
public IEnumerable<Classification> Classifications => _classifications.AsReadOnly();
}
另一半:
public class Classification
{
public Classification(Guid id, Guid propertyId, int classificationBatchId)
{
_id = id;
PropertyId = propertyId;
ClassificationBatchId = classificationBatchId;
_classificationResults = new List<ClassificationResult>();
}
private Guid _id;
public Guid Id => _id;
public Guid PropertyId { get; private set; }
}
ef 核心的配置在这里:
{
public void Configure(EntityTypeBuilder<Property> builder)
{
builder.ToTable(ConfigConstants.PropertyTableName, ConfigConstants.VacantsSchema);
builder.HasKey(p => p.Id);
builder.Property(p => p.Id).HasColumnName("Id").ValueGeneratedNever();
builder.HasMany<Classification>("_classifications").WithOne().HasForeignKey(c => c.PropertyId);
builder.Navigation<Classification>(p => p.Classifications).UsePropertyAccessMode(PropertyAccessMode.Field).HasField("_classifications");
}
}
然而,当我进行迁移时,它会创建一个 propertyId1 列并将 FK 附加到该列以及 PropertyId 字段,知道为什么要创建这个新列吗?
edit i think its something to do with the nullability(?) of the column and the FK, in the Classification object, PropertyId is a Guid and set to non nullable, the PropertyId1 one it always wants to create is a nullable Guid, but im not sure how to force it to be non nullable, or just use the property i tell it to?
EF Core creating shadow properties despite FK being configured
问题是这里
builder.HasMany<Classification>("_classifications") // <--
.WithOne()
.HasForeignKey(c => c.PropertyId);
FK 与由 _classifications
字段表示的 navigation 关联(根据 EF Core 模型术语,它是 navigation 属性 事件虽然按 CLR/C# 术语来说它是一个字段)。因此,由 public IEnumerable<Classification> Classifications
表示的关系未映射,EF Core 假定它是单独的关系并将其映射到常规影子 FK(和列)名称,在这种情况下具有数字后缀,因为原始常规名称已被保留其他已经明确映射的关系。
当然你不想要两个关系,所以把FK映射到真实的就可以了属性
builder.HasMany(e => e.Classifications) // collection navigation property
.WithOne() // no inverse reference navigation property
.HasForeignKey(c => c.PropertyId); // FK property
下一行只是将 属性 与支持字段相关联并设置 EF 使用的访问模式。由于这些是 EFC 3.0+ 的默认值,因此可以安全地跳过它(不需要,多余)
builder.Navigation(e => e.Classifications)
.UsePropertyAccessMode(PropertyAccessMode.Field)
.HasField("_classifications");