“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 1375
春和景丽
春和景丽 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-18 17:25

    Found the problem. In my model, I had a property with a custom enum type, which was in my business project. In my service project, I had my data model project referenced but not the business project. So adding a reference to the model AND business project allowed me to add scaffold controllers fine.

    Seems obvious I know, but the error message it gives you is so unhelpful!

    Anyway, I hope this helps anyone having the same problem, and can't fix it using the other suggestions.

    0 讨论(0)
  • 2021-01-18 17:31

    This answer depends on many things. On my case I didn't have a problem with the model itself. After long hours looking I discovered that another project, which was being referenced on my MVC .NET website, was the one stopping the scaffolding from running correctly.

    What I am doing (kind of tedious) is to remove the reference (breaks many things meanwhile), Add the controllers/views that I need, then add the reference again and voila. No "Object Reference" error.

    Hope this works for you

    0 讨论(0)
  • 2021-01-18 17:33

    If you have more than one project in your solution try to rebuild all.

    0 讨论(0)
  • 2021-01-18 17:37

    I am adding an answer as my problem was the same and my solution was different. It had no relation to the referenced assemblies. First of all, I believe it only happened because the Web API project had just been created (from scratch).

    I will give you some code examples based on the OP code. First, my context class that inherits from DbContext had to call the base constructor passing as argument the connection string name:

    public class PropertySearchContext : DbContext 
    {
        public PropertySearchContext () : base("name=PropertySearchContext")
    

    Second, the Web API application would internally look inside Web.config for a connection string named PropertySearchContext. Web.config already comes with a DefaultConnection, so I just had to add a new one with the proper name and settings:

      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\DATABASE_NAME.mdf;Initial Catalog=DATABASE_NAME;Integrated Security=True" providerName="System.Data.SqlClient" />
        <add name="PropertySearchContext" connectionString="Data Source=MERCURY\SQLEXPRESS;Initial Catalog=DATABASE_NAME;Integrated Security=False;User ID=dbFakeUser;Password=fakePassword;Connect Timeout=10" providerName="System.Data.SqlClient" />
      </connectionStrings>
    

    note: the OQ already has that connection string.

    Third and finally, I had to build the WebAPI project. Once successful, the creation of controllers worked fine.

    0 讨论(0)
提交回复
热议问题