Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 Scaffolding Add Controller gives error "Unable to retrieve metadata..."

I'm using RTM version of Windows 8 and VS 2012 Ultimate. I have a MVC4 project using SqlCe 4.0 with a code first entity framework model.

Model is very simple:

   public class MyThing
    {
        public int MyThingId { get; set; }

        public int UserId { get; set; }
        public string Title { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
  }

When I try to create a new controller with the built in scaffolding too I get the following error:

"Unable to retrieve metadata for MyThing"

"Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used.

How do I get scaffolding to work?

like image 936
Jack Ukleja Avatar asked Aug 28 '12 18:08

Jack Ukleja


4 Answers

By trial and error I found the line of code (it's the DbContext ctor) that is causing the error:

public class MyThingDb : DbContext
{
    // If I comment this constructor out the scaffolding works
    public MyThingDb()
        : base("DefaultConnection")
    {
    }

    public DbSet<MyThing> Things{ get; set; }
}

WTF?

like image 136
Jack Ukleja Avatar answered Sep 30 '22 15:09

Jack Ukleja


I also stumbled into this symptom while running a tutorial on the subject of building an MVC Music Store application.

There definitely seem to be a bug within Visual Studio. What seems to trigger this bug is choosing some name, other than the default, used for the connection string.

My thanks goes to user dwaynef on http://forums.asp.net/t/1838396.aspx/1 for finding this workaround.

A bit elaborated you need to, temporarily during addition of the new scaffolding controller, change the name of your connection string to 'DefaultConnection' in web.config:

<connectionStrings>
     <add name="DefaultConnection" ... />
</connectionStrings>

If you have more than one connection string - make sure only this one is there while performing the action.

like image 35
Henrik Avatar answered Sep 30 '22 17:09

Henrik


Here's my two cents worth. I don't believe your solution actually addresses the real issue. The real fix is to pass the base constructor the database name rather than the connection string name so if your connection string is

<add name="MyContext" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" providerName="System.Data.SqlServerCe.4.0" />

you're context class should be defined as

    public class MyContext : DbContext 
{
    public MyContext() : base("MyDatabase") { }... 

Hope this works for you and others as it does for me.

like image 8
Mr Nice Avatar answered Sep 30 '22 17:09

Mr Nice


Here is what worked for me:

  • Go to connection string in web.config change the following: providerName="System.Data.SqlClient"

instead of

providerName="System.Data.SqlServerCe.4.0"

  • Generate your controller.
  • Rename providerName back to "System.Data.SqlServerCe.4.0".
  • run your project.
like image 5
Desolator Avatar answered Sep 30 '22 16:09

Desolator