Error when creating a Controller in Visual Studio 2012

I’m trying to learn ASP.NET MVC so I’m following the Music Store tutorial on the asp.net website.

I’m at the part where you create the StoreManagerController using Album.cs as the model class and MusicStoreEntities.cs as the data context class.

The error when I create the Controller is: Unable to retrieve metadata for ‘MvcMusicStore.Models.Album’. 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.

Sounds like I’m using two different databases, but here is my connection string section from Web.config:

<connectionStrings>
<add name="MusicStoreEntities" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|/MvcMusicStore.sdf"/>
</connectionStrings>

The odd thing is if I comment out the MusicStoreEntities connection string and then try to create the StoreManagerController it works. It also works if I change the Target Framework in the solution settings to .NET Framework 4, opening the solution in Visual Studio 2010, and then creating the Controller.

So is it an issue with Visual Studio 2012 or the connection string? Maybe some compatibility issue with CE4.0 and VS2012?

Error when Creating a Partial View in Visual Studio 2012 Express with Razor

I am creating a view in Microsoft Visual Studio 2012 Express and I want it to be a strongly-typed partial view, but I am getting the following error: C:/Program Files(x86)/Microsoft Visual Studio 11.

Visual Studio 2012 Test Controller Connection Issue

We have a Visual Studio 2012 test controller. When attempting to connect from a machine with Visual Studio 2012 installed, I can connect to the test controller and queue a test with no issues. When I

Getting error while creating virtual directory throug visual studio 2012

Getting error while creating virtual directory throug visual studio 2012 Unable to create the virtual directory. The URL’http:..localhost:80′ is already mapped to a different folder on an IIS Express

Visual Studio 2012 Update 3 Error

I try to install Visual Studio 2012 Update 3. Currently I have Visual Studio 2012 Update 2 installed. When I try install Update 3 I get error: Visual Studio 2012 Update 3 (KB2707250) has stopped work

Visual Studio 2012 Beta – Error when deploying the app

I have installed Windows 8 OS RP version and visual Studio 2012 beta version. But i’m getting the following error when deploying the package. Error: Windows cannot install package xxxxx, because thi

Visual Studio 2012 Error

I use Visual Studio 2012 on a Windows 8 PC. I had no problems when installing VS2012. When I’m working on a ASP.NET c# project I usually use the option See in the explorer instead debug the project.

Visual Studio 2012 loading error

I have an error when I load Visual Studio 2012: An exception has been encountered. This may be caused by an extension. And here is the message from the xml file: System.ComponentModel.Composition.Co

Error when convert Libjson 7.6.1 to Visual Studio 2012

The latest version of Libjson is 7.6.1, there is a known problem with closing tag on line 239. After fixing that when I try to convert this project from Visual Studio 2012, an error will happen: libjs

unable to launch configure visual studio development web server error in visual studio 2012

I was using Visual Studio 2012 it was working nice, but when I install Visual Studio 2013 and then try to run a project in Visual Studio 2012, it gives me this error unable to launch configure visual

Visual Studio 2012 “Syntax Error”

I’m getting the following errors when I attempt to compile my project in Visual Studio 2012: 1>—— Build started: Project: ConsoleApplication1, Configuration: Debug Win32 —— 1> main.c 1&g

Answers

Duplicate of Add Controller in MVC4 not working and MVC4 Scaffolding Add Controller gives error “Unable to retrieve metadata…”

Try comment your connection string and use default when create controller.

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)/v11.0;Initial Catalog=MvcMusicStore;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|/MvcMusicStore.mdf" providerName="System.Data.SqlClient" />

After creation return your connection string.

Problem lies within using the CE edition of database used in tutorials. When switched to other DB version like SQL server 2012, it worked well for me. Try this:

Change this connection string

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

With

 <add name="MusicStoreEntities" connectionString="Data Source=DBInstanceName;Initial Catalog=MvcMusicStore;Integrated Security=True" providerName="System.Data.SqlClient" />

I think I found a workaround. Before generating a new controller, delete the connection string from Web.config. That will allow you to generate controller without error. Then put the connection string back. That will allow you to run the application.

I am using SQL CE4, and I don’t have an explicit constructor for the DbContext (MusicStoreEntities to use example above). My connection string matches DbContext class name.

In your derived context class you need to pass the database name to the base constructor rather than the connection string name which seems to be the source of confusion for most people having this problem so in your case with connection string

NOTE: Also removed backslash before MvcMusicStore.sdf as not sure it’s necessary

you’re derived context class should be defined as

public class MusicStoreContext : DbContext 

{ public MusicStoreContext() : base(“MvcMusicStore”) { }…

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

Deleting connection string also worked for me, just paste it back in after controller has been created and build project

I’m also just now following this example here: Accessing Your Models Data from a Controller. It’s bold marked there but I was able to fly over it at a first glance:

You ONLY have to build the solution in order to get the Models and DBContexts show up in the “Add Controller” wizard!

This might be easier to read…. The problem is that the connection string from the tutorial is missing the ‘/’ in front of the sdf filename.

bad:

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

good:

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

Yes, such a simple fix!!..Deleting connection string and paste it back in after controller has been created and build project. The Server Explorer Data Connections are not necessary. In the App_Data SHOW_ALL, a type of .sdf file is created after the build and first debug run. This is loaded onto the Web Hostsite as a file, and not a SQL DB.