[英]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.