Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
NewShinyCD Avatar asked Sep 07 '12 18:09

NewShinyCD


People also ask

How do I add a controller in Visual Studio 2012?

In Solution Explorer, right-click the Controllers folder and then select Add Controller. Name your new controller "HelloWorldController". Leave the default template as Empty MVC controller and click Add. Notice in Solution Explorer that a new file has been created named HelloWorldController.

How do I add a controller in Visual Studio?

Using the Add Controller Menu Option The easiest way to create a new controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the Add, Controller menu option (see Figure 1). Selecting this menu option opens the Add Controller dialog (see Figure 2).

What is default controller in MVC?

Default Controller is “HomeController” and “Index” is view.

How do you add a model to a controller?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.


2 Answers

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" />
like image 51
Nexus23 Avatar answered Nov 08 '22 14:11

Nexus23


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.

like image 45
Bohdan Lyzanets Avatar answered Nov 08 '22 13:11

Bohdan Lyzanets