简体   繁体   English

EF7代码优先-生成迁移时,实体类型“对象”上不能存在属性

[英]EF7 Code First - Property cannot exist on entity type 'object' when generating migration

Problem 问题

When trying to create a initial migration of my EF7 Code First database by executing from the command line: 尝试通过从命令行执行来创建我的EF7 Code First数据库的初始迁移时:

dnx ef migrations add Initial

I get the error: 我收到错误:

System.InvalidOperationException: The property 'ExerciseTemplateId' cannot exist on entity type 'object' because the property is not marked as shadow state and no corresponding CLR property exists on the underlying type. System.InvalidOperationException:属性'ExerciseTemplateId'不能存在于实体类型'object'上,因为该属性未标记为阴影状态,并且基础类型上不存在相应的CLR属性。 Full Stacktrace 完整的堆栈跟踪

Any ideas? 有任何想法吗?

The Model Causing the Error 导致错误的模型

For some reason EF7 doesn't seem to like the primary key property ExerciseTemplateId on my Model: 由于某些原因,EF7似乎不喜欢我的模型的主键属性ExerciseTemplateId

public class ExerciseTemplate
{
    public int ExerciseTemplateId { get; set; }
    public string InitalCode { get; set; }
    public string ClassName { get; set; }
    public string MainMethodName { get; set; }

    public int ExerciseForeignKey { get; set; }
    public Exercise Exercise { get; set; }
}

More Detail 更多详情

The only interesting part of the ExerciseTemplate model is a one to one relationship with the Exercise model: ExerciseTemplate模型中唯一有趣的部分是与Exercise模型的一对一关系:

public class Exercise
{
    public int ExerciseId { get; set; }
    public string Name { get; set; }
    public string Guidance { get; set; }
    public ExerciseTemplate Template { get; set; }
    public List<ExerciseCategory> Categories { get; set; }
    public List<Test> Tests { get; set; }
}

This question is already long so my DBContext is in this DBContext Gist 这个问题已经很长了,所以我的DBContext在此DBContext要点中

Update 更新资料

All the Models and the DB Context can be found at this Models and Context Gist 可以在此模型和上下文摘要中找到所有模型和数据库上下文

Long story short: do not use object or Type as property type in your model classes. 长话短说:不要在模型类中使用objectType作为属性类型。 It has no representation in the database. 它在数据库中没有表示形式。

If you absolutely need to store arbitrary objects, you may use some serializer and store the serialized objects as BLOB in the database. 如果绝对需要存储任意对象,则可以使用一些序列化程序,并将序列化的对象存储为BLOB在数据库中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM