21

I am learning ASP.NET MVC from tutorials of Microsoft :

http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-https://stackoverflow.com/editing-helpdata-from-a-controller

At the link above mentioned, while adding a controller named "MoviesController" , i am getting this error

"Unable to retreive metadata for 'MvcMovie.Models.Movie'. 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 can i fix that?

Community
  • 1
  • 1
Nomi Khan
  • 1,177
  • 4
  • 11
  • 15
  • yea i got the same error. Hope anybody can help. Are you using visual studio 12? – darkdog Sep 14 '12 at 07:15
  • YEAH i am using Visual studio 2012 – Nomi Khan Sep 14 '12 at 07:40
  • I had the same problem. I found the solution here: http://stackoverflow.com/questions/12172074/add-controller-in-mvc4-not-working – Manu Jan 11 '13 at 12:27
  • I had the same error. I found the solution here: http://stackoverflow.com/questions/12172074/add-controller-in-mvc4-not-working – Manu Jan 11 '13 at 12:30

13 Answers13

25

I had the same issue.

I switched providerName="System.Data.SqlServerCe.4.0" with providerName="System.Data.SqlClient", and it created the Controller and Views.

I found this: http://msdn.microsoft.com/en-US/library/ms171861.aspx

I followed the directions and added a reference to SQL Server Compact, but it still doesn't work.

I also tried commenting out the default SQL Server Express connection, but it still gave the same error when trying to add the controller.

I'm just going to use SQL Server Express. I will let you know if I have any issues.

hyp3rg3om3tric
  • 449
  • 1
  • 4
  • 10
  • 9
    OK, so switching to `providerName="System.Data.SqlClient"` will get the controller and the views built, but the site won't run. If after using the switch to build the controller, you then switch it back to `providerName="System.Data.SqlServerCe.4.0"`, it will then run. It's not pretty, but it should get you through the tutorial. – hyp3rg3om3tric Sep 20 '12 at 00:37
  • I was able to finally get it working by using the original connection string, but adding a reference to "System.Data.SqlServerCe" (version 4.0, not 3.5). – Justin May 30 '13 at 21:00
6

I've found that using the following works: (Assuming that you've got SQL Server Express or higher installed)

 <add name="MovieDBContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;AttachDBFilename=|DataDirectory|MovieDB.sdf"/>
Ha L.
  • 71
  • 1
  • 7
2

I think the reason of this error is because the VS is having some difficult to reuse your existing DBContext, while scaffolding. The VS try to use a property in DBContext with similar name of the Domain (like trying the Domain name concatenating "s"). If it cant find the property and your context already has a DBSet with you Domain class, it generates that error.

My solution was to create a new DBContext named "DeleteContext". After creating with success the controller and views, I have replaced the "DeleteContext" in my Controller to my existing one. Finally I deleted the "DeleteContext" class.

It works really fine.

mqueirozcorreia
  • 905
  • 14
  • 33
1

That solution was not working for me, it blows on return View(db.Movies.ToList());

Instead use this :

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

and this:

public class MovieDBContext : DbContext
{
   public MovieDBContext() : base("Movie") { }
   public DbSet<Movie> Movies { get; set; }
} 
Obaid
  • 1,407
  • 19
  • 36
1

I ran into the same error on another Microsoft ASP.NET MVC tutorial while using Visual Studio 2012 (http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5). I decided that for the sake of completing the tutorial, it was easier to just use SQL Express in place of SQL CE.

What I did to resolve the issue was delete MvcMusicStore.sdf from Server Explorer, then deleted the same database file from App_Data in Solution Explorer.

I updated the connection strings section in the Web.config to use a Sql Express database (.mdf) in place of Sql CE (.sdf). For this particular tutorial, MusicStoreEntities is the name of the class that extends DbContext:

<connectionStrings>
    <add name="MusicStoreEntities" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

I built the solution, ran the site, and the database was regenerated for me. To add the database file back to the solution, click the "Show All Files" button in Solution Explorer, right-click the .mdf file in App_Data, and select "Include in Project."

Todd L
  • 91
  • 1
  • 3
1

Hi there's one solution that worked fine to me. In your Web.config, the tutorial told you to add the following line in the connectionStrings section:

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

Do not delete it or change it!

When you're adding a new controller you can make this to avoid the error that is presenting:

  1. Comment the line above.
  2. Add the following line: <'add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlClient"/>
  3. Save your solution
  4. Add the controller
  5. Before you run your app, uncomment the line you have commented (1), and comment the line you added (the one with the providerName="System.Data.SqlClient") (2).

This worked very fine to me, when adding the controllers.

Hope this could help you.

Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77
0

I just tryed a bit around and found the problem.

In the tutorial you are adding the following line in your web.config

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

It seems that there is a problem with it.. i just commented it out and used the SqlServer and it works for me. If you still want to use the SqlServerCe you need to take a look how to fix that problem.

darkdog
  • 3,805
  • 7
  • 37
  • 47
  • 1
    i need to figure out why it is producing error on using SQLServerCe, if it will cause the error then why Microsoft is mentioning in that manner, or why they have not discussed about this problem – Nomi Khan Sep 14 '12 at 11:53
0

If you're using VS 2012, you will need to also tell EF to use SQL Compact instead of localDb.

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="System.Data.SqlServerCe.4.0" />
  </parameters>
</defaultConnectionFactory>

An easier way is to install the EF SQL Compact Nuget package.

For more details, check out this blog entry.

sean
  • 11,164
  • 8
  • 48
  • 56
0

I ran into this same problem while working through the Contoso University asp.net MVC tutorial. It appears that the problem comes from mixing the SQL Server Compact connection strings with the Membership provider Sql Server connection.

I initially used hyperGeoMetric's fix, and that did work. Then I looked at the downloadable code's web.config and noticed some additional configuration.

If you add/replace the default parameter of entityFramework with this:

<parameters>
        <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>

add a system.data section like this:

<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.4.0" />
      <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>

and modify the existing (default) DefaultConnection to look like this:

<add name="DefaultConnection" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=|DataDirectory|aspnet-membership.sdf" />

After these modification, I was able to continue the tutorial using the Sql Server Compact edition.

Sam

Nostradamnit
  • 862
  • 1
  • 10
  • 20
0

I followed that tutorial too, and got the same issue. I deleted the connection string, then I was able to add the controller, and it created the other files. Then I added the same connection string again to the Web.config file, inserted it ecxactly to the same place where it was before. It solved my problem.

RcsAkos
  • 1
  • 2
0

I'm learning this tutorial:http://mvcmusicstore.codeplex.com/. and got the same error. I found a item name "EntityFramework.SqlServerCompact" in the NuGet packages list, and install it, all things go fine! note, the packages dependent entityframework, you can view all the version here:https://www.nuget.org/packages/EntityFramework.SqlServerCompact. GOOD LUCK!

cablefan
  • 61
  • 5
0

the path of data source is wrong , you can add a "\" before "Movie.sdf". like this:

<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|\Movie.sdf" providerName="System.Data.SqlServerCe.4.0"/>
-1

I ran into the same problem in another solution. It appeared just after I introduced a parametrized call to the base constructor, like the one included in the default UsersContext class.

This fails

public class MovieDBContext : DbContext
{
  public MovieDBContext() : base("DefaultConnection")
  {
  }

  ...
}

This works

public class MovieDBContext : DbContext
{
  // No constructor here
  ...
}

It seems that naming the connection string in the constructor creates the error. I only have one connection string in my web.config, so DefaultConnection is still used, although I don't name it explicitly.