“Object reference not set to an instance of an object” when creating a new Web API controller with EF Scaffolding in Visual Studio 2012

后端 未结 5 1374
春和景丽
春和景丽 2021-01-18 16:42

I have an MVC4/Web API project, with an Entity Framework, Code First data model. When I try to create a new API Controller with read/write methods using a data context &

5条回答
  •  -上瘾入骨i
    2021-01-18 17:17

    I get this error if I am attempting to Scaffold a controller with views for a Model that does not contain a default constructor(one that does not need parameters). Add one to the model and try again. This has bitten me more than once. The newer compilers works just fine without one, as if it does not see one it just inserts one for you. However the scaffold operation needs that default constructor be actually declared in code to operate correctly. I just wish it gave you a better explanation when you get the error.

    So for the original post simply adding this to the class :

    public Property(){}
    

    should do the trick.

提交回复
热议问题