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

asp.net-core-mvc c# entity-framework-core

Question

Problem

When trying to create a initial migration of my EF7 Code First database by executing from the command line:

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. 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:

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:

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

Update

All the Models and the DB Context can be found at this Models and Context Gist

1
0
3/22/2016 6:45:24 PM

Accepted Answer

Long story short: do not use object or Type as property type in your model classes. 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.

1
3/22/2016 7:43:36 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow