16

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.

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
user1632145
  • 189
  • 1
  • 1
  • 5
  • 1
    possible duplicate of [MVC4 Scaffolding Add Controller gives error "Unable to retrieve metadata..."](http://stackoverflow.com/questions/12165185/mvc4-scaffolding-add-controller-gives-error-unable-to-retrieve-metadata) – Darin Dimitrov Aug 29 '12 at 06:49
  • No cause in my DbContext I donot have a constructor which refers to DefaultConnection.. – user1632145 Aug 29 '12 at 07:13
  • I have the same problem and I am using Windows 8. – Fontanka16 Sep 03 '12 at 21:17

11 Answers11

28

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.

Jake
  • 733
  • 8
  • 23
Arnaldo237
  • 306
  • 2
  • 2
  • did not work for me :( gives **connection string cannot be null or empty string** error. also are the double asterisks at connection string name and database name for emphasis or required by SQL CE? – TheVillageIdiot Oct 15 '12 at 12:22
  • +1 worked for me as well! @TheVillageIdiot The formatting of this answer is a bit screwy. I tried to fix it but I couldn't make it happy. The `**` are intended to bold the text in the post for emphasis and should not be included in the connection string. – JohnnyHK Jan 07 '13 at 18:27
  • 1
    @JohnnyHK yes ** is to make things **bold** but it does not works in code-blocks – TheVillageIdiot Jan 07 '13 at 23:09
9

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Thomas Bindzus
  • 1,508
  • 12
  • 12
  • 1
    This removes the error and allows the controller to be added, but making only this change - without adding the EntityFramework.SqlServerCompact package - seems to cause EF to use SQL Express instead. – Kimberly Oct 25 '12 at 19:21
6

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+

Himanshu
  • 31,810
  • 31
  • 111
  • 133
robert
  • 61
  • 1
  • Thank you so much! This helped me - just commented the connection string, added the controller, uncommented it. Done! Looks like there's some problem with the SQL CE part in my connection string. – Oliver Mar 26 '13 at 20:57
5

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".

Jaanus
  • 16,161
  • 49
  • 147
  • 202
2
  1. Create the Controller
  2. Add the String Context in web.config

It's a chronological problem not a bug. Source : from Vô Thường's comm http://www.itorian.com/2012/10/unable-to-retrieve-metadata-for.html

WOlF
  • 115
  • 1
  • 8
2

A note if it helps anyone. I just had this problem.

This Fixed it: 1. Commenting out the Connection String 2. Rebuild Website without Connection String 3. Add Controller 4. Uncomment Connection String 5. Rebuild Website

All Works Fine.

ChrisB
  • 21
  • 1
2

I was having this problem when adding a API 2.0 / MVC5 controller with Entity Framework 6 and an MySQL database.

[DbConfigurationType(typeof(MySqlEFConfiguration))]

Comment this class attribute out. Build project. Then adding a controller works for me on MySql.

Uncomment after controller is added and continue as normal.

David Graham
  • 389
  • 5
  • 15
1

This is going to seem like the dumbest thing, but do you have a connection made to your database elsewhere? I was running into this same exact problem. I had the database open in the Server Explorer view (I had made a connection to my SDF file). Once I closed this connection, everything worked perfectly.

JasCav
  • 34,458
  • 20
  • 113
  • 170
1

Ok, this is my evil solution to the problem. I finally made it work. Win8 VS2012 EF5 MVC4

  • Make sure you have the Nuget package EntityFramework.SqlServerCompact installed
  • At first, do not add the DbContext manually. Instead, use the from the scaffolding menu
  • When you have created one Controller+Views using scaffolding, switch back to your old connectionstring in the web.config - the one using SQL CE 4.0. Then everything works fine for me
Fontanka16
  • 1,161
  • 1
  • 9
  • 37
1

I feel like this is related, but I used the solution above to fix the problem and let me build controllers again. The problem I had after that was that I couldn't seem to find my SQL Compact database after that in server explorer. It appeared to be working when I added the application, but I couldn't find it. Once I removed the constructor: public class SchoolContext : DbContext { public SchoolContext() : base("School") { } .

I was able to see the db again. I'm still in development and need to add more controllers, so I'll have to use it again, I guess. But this took me all morning to figure out why I suddenly couldn't see the database in server explorer anymore.

mgordon12
  • 41
  • 1
  • 4
1

If you're using VS 2012, you will need to also tell EF to use SQL Compact instead of localDb.

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
</defaultConnectionFactory>

An easier way is to install the EF SQL Compact Nuget package.

For more details, check out this blog entry.

sean
  • 11,164
  • 8
  • 48
  • 56