简体   繁体   中英

Add Controller in MVC4 not working

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:

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

我将连接字符串的名称改回DefaultConnection,然后它起作用了,我首先尝试添加默认构造函数,但收到一些关于没有正确的程序集引用的警告。

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

  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

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.

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.

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.

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM