NewShinyCD
NewShinyCD

Reputation: 175

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?

Upvotes: 14

Views: 11287

Answers (8)

JJean
JJean

Reputation: 21

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.

Upvotes: 2

Nexus23
Nexus23

Reputation: 6373

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

Upvotes: 19

Jeff
Jeff

Reputation: 11

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

Upvotes: 1

Kjellski
Kjellski

Reputation: 925

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!

Upvotes: 1

hoektoe
hoektoe

Reputation: 71

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

Upvotes: 0

Mr Nice
Mr Nice

Reputation: 227

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.

Upvotes: 3

Felix
Felix

Reputation: 10078

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.

Upvotes: 2

Bohdan Lyzanets
Bohdan Lyzanets

Reputation: 1672

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.

Upvotes: 8

Related Questions