21

I want to add controller in my MVC 4 application in VS2012 as this image:

add controller

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcDemo.Models
{
    public class MovieDB
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Director { get; set; }
        public DateTime Date { get; set; }
    }
    public class MovieDBContext : DbContext
    {
        public DbSet<MovieDB> Movies { get; set; }
    }
}

Connection strings:

<connectionStrings>

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

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

</connectionStrings>

After clicking "add", this error occurs:

unable to retrieve metadata for 'MvcDDemo.Models.MovieDB'.Using the
same DbCompiledModel to create contexts against different type of
database servers is not supported.instead,create a 
separate DbCompiledModel for each type of server being used.

Any suggestion?

Majid
  • 13,853
  • 15
  • 77
  • 113
  • 1
    the obvious part is your context is failing. the not obvious part is **why**. this is usually, in descending order, 1) connection string in web.config, 2) your db 3) since you seem to be using Code First, your initialization method or migration strategy could be the problem. Please explain what you have done to intialize database, what kind of db, anything relevant... – Dave Alperovich Mar 16 '13 at 05:10
  • ok, share your connection string – Dave Alperovich Mar 16 '13 at 06:19
  • 1
    it's definitely a problem with connecting to db, but I don't see it. you're using compact and your connection looks solid. you're using the right context... – Dave Alperovich Mar 16 '13 at 07:03
  • In my case I have installed EF6 on my web project and EF5 on my model project. – Ε Г И І И О Nov 18 '13 at 20:16

8 Answers8

22

In Web.config, set second providerName same as first providerName, and after creating controller, undo that!

from:here

Community
  • 1
  • 1
Majid
  • 13,853
  • 15
  • 77
  • 113
5

Change providerName="System.Data.SqlServerCe.4.0 to providerName="System.Data.SqlClient to fix this error.

musicakc
  • 488
  • 1
  • 4
  • 15
5

My post may help in case someone has the same problem.

I tried experimenting with EF6 and EF5 in the same project, and I noticed that the <entityFramework> tag was messed up, and got the same problem above.

Here is what I did, and it solved the problem:

  1. I removed the <entityFramework> tag from Web.config
  2. Uninstalled entity framework from my MVC app using nuget
  3. Installed entity framework to my MVC app again

After removing the <entityFramework> tag and re-installing EF5 it was fixed, and I can scaffold my controllers again.

Haitham Sweilem
  • 1,633
  • 1
  • 12
  • 13
3

Don't forget to clean and rebuild before you try to scaffold! That was my mistake.

Scuba Steve
  • 1,541
  • 1
  • 19
  • 47
0

In web.config, delete the AttachDBFilename=|DataDirectory|****.mdf line.

Majid
  • 13,853
  • 15
  • 77
  • 113
0

User following for providerName = "System.Data.SqlServerCe.4.0"

<add name="MovieDBContext" 
connectionString="Data Source=|DataDirectory|\Movies.sdf"
providerName="System.Data.SqlServerCe.4.0" />
Majid
  • 13,853
  • 15
  • 77
  • 113
HPP
  • 244
  • 2
  • 12
0

I had the same issue and below is the change which fixed the issue for me. Had to change localhost to . And Added Initial catalog=Movies.

Before

<add name="MovieDBContext" connectionString="Data Source= localhost\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MoviesDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

After (fix)

<add name="MovieDBContext" connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=Movies; AttachDbFilename=|DataDirectory|\MoviesDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

Reference:

ASP .Net MVC 4, Invalid value for key 'attachdbfilename'

Community
  • 1
  • 1
marak
  • 260
  • 3
  • 19
0

Make sure that the context section has access to the context created in that class library. for example: enter image description here

Persistence.DbContexts is not Correct you must use this Persistence.DbContexts.DataDbContext