I want to add controller in my MVC 4 application in VS2012 as this image:
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?
Don't forget to clean and rebuild before you try to scaffold! That was my mistake.
In
Web.config
, set secondproviderName
same as firstproviderName
, and after creating controller, undo that!from:here
In
web.config
, delete theAttachDBFilename=|DataDirectory|****.mdf
line.Change
providerName="System.Data.SqlServerCe.4.0
toproviderName="System.Data.SqlClient
to fix this error.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:
After removing the <entityFramework> tag and re-installing EF5 it was fixed, and I can scaffold my controllers again.
User following for
providerName = "System.Data.SqlServerCe.4.0"