MVC4 Scaffolding Add Controller gives error “Unable to retrieve metadata…”

我的未来我决定 提交于 2019-11-26 11:09:16

问题


I\'m using RTM version of Windows 8 and VS 2012 Ultimate. I have a MVC4 project using SqlCe 4.0 with a code first entity framework model.

Model is very simple:

   public class MyThing
    {
        public int MyThingId { get; set; }

        public int UserId { get; set; }
        public string Title { get; set; }
        public string Address { get; set; }
        public string Description { get; set; }
        public DateTime Date { get; set; }
  }

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

\"Unable to retrieve metadata for MyThing\"

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

How do I get scaffolding to work?


回答1:


By trial and error I found the line of code (it's the DbContext ctor) that is causing the error:

public class MyThingDb : DbContext
{
    // If I comment this constructor out the scaffolding works
    public MyThingDb()
        : base("DefaultConnection")
    {
    }

    public DbSet<MyThing> Things{ get; set; }
}

WTF?




回答2:


I also stumbled into this symptom while running a tutorial on the subject of building an MVC Music Store application.

There definitely seem to be a bug within Visual Studio. What seems to trigger this bug is choosing some name, other than the default, used for the connection string.

My thanks goes to user dwaynef on http://forums.asp.net/t/1838396.aspx/1 for finding this workaround.

A bit elaborated you need to, temporarily during addition of the new scaffolding controller, change the name of your connection string to 'DefaultConnection' in web.config:

<connectionStrings>
     <add name="DefaultConnection" ... />
</connectionStrings>

If you have more than one connection string - make sure only this one is there while performing the action.




回答3:


Here's my two cents worth. I don't believe your solution actually addresses the real issue. The real fix is to pass the base constructor the database name rather than the connection string name so if your connection string is

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

you're context class should be defined as

    public class MyContext : DbContext 
{
    public MyContext() : base("MyDatabase") { }... 

Hope this works for you and others as it does for me.




回答4:


Here is what worked for me:

  • Go to connection string in web.config change the following: providerName="System.Data.SqlClient"

instead of

providerName="System.Data.SqlServerCe.4.0"

  • Generate your controller.
  • Rename providerName back to "System.Data.SqlServerCe.4.0".
  • run your project.



回答5:


Works for me: In the "Add New Scaffolded Item" dialog, i added a new context (plus) with any name (for me "ScaffoldingContext"). Then the scaffolding works. Just rename the context in the Controller.




回答6:


After trying different options, the below method resolves the error..

If the name value in connection string matches with the value passed to the constructor, it works.

 public MyThingDb()
        : base("name=MyContext")
    {
    }



回答7:


Problem may be because of missing [NotMapped] Attribute in one of the model class.

As I missed the attribute and I was cunning my head.

[Display(Name="Logo")]
[DataType(DataType.Upload)]
[NotMapped]
public HttpPostedFileBase Logo { set; get; }



回答8:


This may sometimes be caused by an association property or a foreign key attribute




回答9:


I had a similar issue but it wasn't the default constructor. It also happens if you have multiple projects in your solution and your "Web" facing MVC project does not reference EntityFramework.




回答10:


Change "Things" inside

public
DbSet<MyThing> Things
{ get; set; }
}

to "Database1" where "Database1" is the name of the database file on disk which appears in your Web.config file as "Database1.sdf"




回答11:


The solution that worked for me is to pass the same database name as in your connection string to the base constactor of your dbContext class.




回答12:


I solved this by pressing CTRL+F5 to rebuild my project before adding my controller.




回答13:


I had a similar issue when trying to create a view from my controller using the scaffolding. In the create view dialog I simply cleared the "Data Context Class" dropdown and then the scaffolding mechanism worded fine.




回答14:


This is what that worked out for me ..

I commented out the connectionstring using the 'System.Data.SqlServerCe.4.0' and then added the controller with scaffolding templates.




回答15:


In your context class you have to comment the DbConfigurationType when you are going to create a controller with scaffolding.

//[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class NameDbContext 
{}



回答16:


MVC 5 / EF 6

perhaps you can do this in the older version?

  1. commented out the connection strings in the web / app.config then save
  2. try to create new controller and have VS create a "new" dbcontext item instead of choosing the one you already have
  3. click create
  4. delete new dbcontext class
  5. replace controller dbcontext with yours
  6. uncomment connection strings in web / app.config then save

worked for me!




回答17:


I had the same error message, but it was nothing to do with the connection string.

It is a very rare case, but hopefully this will help someone. My model name was the same as one of the segments of my namespace name.

For example:

namespace blah.blah.Building

public class Building

I renamed my namespace and fixed all usages and then the t4 scaffolding worked!


Her's another possible solution. You may have to run scaffolding for "dependent" models first. Then work your way up to complicated models that have many dependencies.



来源:https://stackoverflow.com/questions/12165185/mvc4-scaffolding-add-controller-gives-error-unable-to-retrieve-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!