anwar
anwar

Reputation: 91

MVC Scaffolding Issue

When I try to Scaffold My Controller, the page throws following error

"Unable to retrieve metadata for 'Entity.Observation'. No parameterless constructor defined for this object."

Can you help me this?

Here is the code:

 public class Observation
    {
        public Observation() { }

        public virtual int Id { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual User Teacher { get; set; }
        public virtual User Observer { get; set; }
        public virtual AcademicYear AcademicYear { get; set; }
    }

Entities is in other project, Context is in different project and Controllers and Views are in same project

I am using Entity Framework Code First model

Upvotes: 9

Views: 2610

Answers (4)

Harry Mower
Harry Mower

Reputation: 71

I had the same problem. Here's how I fixed -

  1. First, I added another constructor with no parameter to the context class (not the model class).

  2. After that I had to rebuild the project. Really important because the tooling reads the meta data (i burned 10 miuntes before I realized I had to do this).

Once I did those two things, I was able to add the controller and views with no problem.

Upvotes: 4

The Ed R
The Ed R

Reputation: 326

I had the exactly same problem, the error stated that a default (parameterless) constructor was missing from my model. In my case the error was misleading - my model did in fact contain a default constructor but my DataContext did not. I added a default constructor to my DataContext - problem solved!

public class ReportEntities : DbContext
    {

    public ReportEntities():base()
        {

        }

    public ReportEntities(string connection)
        : base(connection)
        {

        }

    ...
     }

Upvotes: 9

mithun_daa
mithun_daa

Reputation: 4384

If you have you EF models in a separate project you can try right clicking the edmx file and "Add Code Generation Items" and choose "DbContext Generator". This is available only if you have EF 4.1 (easiest way to install is to use NuGet). See if that helps. Every time I make a change to my model project I generally make sure i build that project before I start working on the other projects.

Upvotes: 1

Ron Sijm
Ron Sijm

Reputation: 8758

If you in fact did add the parameterless constructor and you're still getting the error, it sounds like your build isn't updating. So try this:

  1. First off all, add a parameterless constructor: public void Observation(){}, or have no constructors at all.

  2. However, scaffolding is done using metadata; reflection. Before you retry you'll have to rebuild the project with the model in. In this case: Observation make sure there are no build errors.

  3. Since you said the entity was in another project, make sure the reference is correct. For example: click the reference to the entities dll, and make sure the path is correct. Also, make sure Copy Local is on false. If its on true; the dll only gets "copied local" one time. You're probably working with an old version.

If it still fails after that, try to delete the entities dll first, and rebuild, so you're 100% sure you're working with a new version.

Upvotes: 0

Related Questions