Add Controller in MVC4 not working

前端 未结 11 543
一向
一向 2020-11-30 08:58

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
    {
       p         


        
相关标签:
11条回答
  • 2020-11-30 09:20

    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.

    0 讨论(0)
  • 2020-11-30 09:20

    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.

    0 讨论(0)
  • 2020-11-30 09:25

    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+

    0 讨论(0)
  • 2020-11-30 09:26

    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.

    0 讨论(0)
  • 2020-11-30 09:27

    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
    0 讨论(0)
  • 2020-11-30 09:28
    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

    0 讨论(0)
提交回复
热议问题