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.
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:
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.
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.
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+
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"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With