简体   繁体   English

为什么 Entity 在已经定义的情况下创建关键字段?

[英]Why is Entity creating a key field when it is already defined?

I have several related domain models that are triggering the exception SqlException: Invalid column name 'ChecklistTemplate_Id' .我有几个相关的域模型触发异常SqlException: Invalid column name 'ChecklistTemplate_Id'

My domain model looks like:我的域 model 如下所示:

public class Assignment
{
    public long Id { get; set; }
    public long ChecklistId { get; set; }
    public DateTime InspectionDate { get; set; }
    public long JobId { get; set; }
    public Guid? EmployeeId { get; set; }
    // TODO: Make the completion a nullable date time in the database and here
    public DateTime CompletionDate { get; set; }

    public virtual Job Job { get; set; }
    public virtual Checklist Checklist { get; set; }
    public virtual IList<Image> Images { get; set; }
    public virtual IList<Attachment> Attachments { get; set; }
    public virtual IList<Equipment> Equipments { get; set; }
}

My EntityTypeConfiguration class looks like:我的 EntityTypeConfiguration class 看起来像:

internal class AssignmentConfiguration : EntityTypeConfiguration<Assignment>
{
    public AssignmentConfiguration()
    {
        ToTable("Assignment");

        HasKey(k => k.Id);

        Property(a => a.ChecklistId)
            .IsRequired();
        Property(a => a.CompletionDate)
            .IsOptional();
        Property(a => a.EmployeeId)
            .IsOptional();
        Property(a => a.Id)
            .IsRequired();
        Property(a => a.InspectionDate)
            .IsRequired();
        Property(a => a.JobId)
            .IsRequired();

        HasRequired(a => a.Job)
            .WithMany(a => a.Assignments)
            .HasForeignKey(a => a.JobId);

        HasRequired(a => a.Checklist)
            .WithOptional(a => a.Assignment);

        HasMany(a => a.Images)
            .WithRequired(a => a.Assignment)
            .HasForeignKey(a => a.InspectionId);
    }
}

The Checklist domain model has a ChecklistTemplate navigation property with the join: Checklist 域 model 有一个带有连接的 ChecklistTemplate 导航属性:

HasMany(a => a.CheckLists)
            .WithRequired(a => a.ChecklistTemplate)
            .HasForeignKey(a => a.ChecklistTemplateId);

There is a one to one between Assignment and Checklist as seen in the Assignment entity configuration.如 Assignment 实体配置中所示,Assignment 和 Checklist 之间存在一对一的关系。

And yes, we are including the configuration in the DBContext.是的,我们将配置包含在 DBContext 中。

Also, I looked at Entity Framework 6 creates Id column even though other primary key is defined and that doesn't seem to apply.此外,我查看了Entity Framework 6 创建 Id 列,即使其他主键已定义并且似乎不适用。

I dont have a satisfactory answer to that but I have had a lot of trouble with ef6.我对此没有满意的答案,但我在使用 ef6 时遇到了很多麻烦。 This is because there is a navigation which is not defined or wrongly defined.这是因为存在未定义或错误定义的导航。 So ef6 creates it on-the-fly on proxy classes and you cry for hours.所以 ef6 在代理类上即时创建它,你会哭几个小时。 I hope you find out the problem soon.我希望你能尽快找出问题所在。

And the navigation you stated is one-to-many.你说的导航是一对多的。 Be careful.当心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体类型&#39;xxx&#39;需要主键,但我已经定义了它? - The entity type 'xxx' requires a primary key,but i already defined it? 实体框架 - 已经定义 - Entity Framework - Already Defined 实体类型“服务”需要定义一个主键。 但我已经定义了 - The entity type 'Service' requires a primary key to be defined. But I already defined it 使用实体框架时,为什么不能访问外键字段的值? - Why can't I access the value(s) of foreign key field(s) when using Entity Framework? 实体框架密钥未定义 - Entity framework key not defined 实体框架可选外键在从属中创建冗余FK字段 - Entity Framework optional foreign key creating redundant FK field in dependant 为什么实体类型”需要定义主键? - Why does the entity type '' require a primary key to be defined? 实体框架:具有架构的实体已经定义 - Entity Framework: The Entity with schema was already defined 与 IdentityUser 一起使用时,Entity Framework Core 拥有的类型值 Object 抛出 required Primary Key to be defined 错误。 为什么? - Entity Framework Core Owned Type Value Object throws required Primary Key to be defined error when used with IdentityUser. Why? WPF:key已经定义在这个scope - WPF: The key is already defined in this scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM