Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Controller in MVC4 not working

Tags:

c#

asp.net-mvc

I'm using VS 2010 Premium. I have a MVC4 project using SqlCe 4.0 with a entity framework model.

Model is:

  public class ProjectBuild
    {
       public int ProjectBuildID {get;set;}
       public string name {get;set;}
    }

  public class ProjectBuildContext:DbContext
     {
       public DbSet<ProjectBuild> builds {get;set;}
     }

Below is my connection string:

 add name="ProjectBuildContext" connectionString="Data Source=|DataDirectory|DB.sdf"
 providerName="System.Data.SqlServerCe.4.0"

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

"Unable to retrieve metadata for ProjectBuild"."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.

like image 644
user1632145 Avatar asked Aug 29 '12 06:08

user1632145


4 Answers

I tried Fontanka16 solution, but it did not work, it turned out that my DbContext class was missing its default constructor defining the target CE database.

These are my steps summary:

  • Installed the Nuget package EntityFramework.SqlServerCompact.
  • Added the default constructor to my DbContext class.

    public class SchoolContext : DbContext { public SchoolContext() : base("School") { } ... }

  • My connection string is:

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

Then it worked.

like image 153
Arnaldo237 Avatar answered Nov 13 '22 12:11

Arnaldo237


I changed the name of my connection string back to DefaultConnection and then it worked, I tried at first to add the default constructor but received some warning about not having the correct assemblies references to.

like image 21
Thomas Bindzus Avatar answered Nov 13 '22 11:11

Thomas Bindzus


I'm new to using EF and MVC, but what I've found is that if you do not define the data connection (comment it out) or rename it in web.config, Visual Studio will allow you to add the controller. If no connection is defined then EF will automatically use SQLExpress. After the controller has been added you can add your connection string back to web.config and the program will function as expected.

//this solution is an excerpt from http://forums.asp.net/t/1838396.aspx/1?Error+while+adding+controller+class+Unable+to+retrieve+metadata+for+MvcMovie+Models+Movie+

like image 6
robert Avatar answered Nov 13 '22 12:11

robert


You should always initialize the superclass with base(string). Also found out that the name of connectionString in Web.config may not be same as the String you send to the super class contructor.

So for example :

<add name="MovieDBContext"  ....... />

public class MovieDBContext : DbContext
{
   public MovieDBContext() : base("Movie") { }
}

So you see, the "MovidDBContect" is different from "Movie".

like image 5
Jaanus Avatar answered Nov 13 '22 11:11

Jaanus