25

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
  • There is a specific solution down by - Suplanus if you came here as a result of MySql not SqlCe
    – A-Majeed
    Commented Jan 1, 2016 at 1:37

17 Answers 17

11

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?

8
  • 6
    Is this an answer, or part of the question? Commented Aug 28, 2012 at 18:47
  • Yes its a good string within web.config, but it must be that the scaffolding code has problems accessing it (just guessing) Commented Aug 28, 2012 at 21:15
  • It says "I found the line of code... that is causing the error" so yeah its the solution. Cant mark is the an "answer" for another couple of days sorry. Commented Aug 28, 2012 at 21:17
  • I am running into the EXACT SAME ERROR as you - however commenting out my constructor did not fix it: stackoverflow.com/questions/12546545/…
    – 99823
    Commented Sep 22, 2012 at 21:45
  • @Loren Presumably you did a rebuild after the commenting out? Rebuild before running the scaffolder Commented Sep 24, 2012 at 17:50
9

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.

1
  • Commenting the second, non-DefaultConnection connectionString worked for me :) Commented Feb 5, 2013 at 18:31
8

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.

1
  • I think this will create another new database in the \SQLEXPRESS instance in the user's machine. This is more like a substitute than solution.
    – lbrahim
    Commented Oct 22, 2013 at 9:11
5

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.
1
  • I don't remember why exactly, but probably because there are some bugs in System.Data.SqlServerCe.4.0
    – Desolator
    Commented Sep 25, 2013 at 6:15
4

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.

3
  • This is the best solution when working with MySql as well, seems to eliminates all the MySql specific scaffolding problems.
    – Derrick
    Commented Dec 15, 2015 at 15:45
  • Exactly, THIS IS THE SOLUTION when having the Issue With MySQL.
    – A-Majeed
    Commented Jan 1, 2016 at 1:35
  • This worked for me too,BUT only for View not the controller!!! funny thing is that I just created a controller for another model and it was ok! but the second model had this issue!!!
    – Exlord
    Commented Sep 9, 2017 at 6:06
2

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")
    {
    }
2

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; }
2

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

1
  • This Happened to me. I had to comment out all the [ForeignKey("reference")] annotations to make it work. Commented May 3, 2017 at 12:07
1

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.

1

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"

1

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.

1

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

1

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.

0

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.

0

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 
{}
0

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!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.