This is an important issue which sometimes most of the beginners have face when they start working with SQL Server 2008 database in ASP.Net MVC 4 EF code-first architecture!
Here, we will learn an easy solution to this issue.
After creating a project in ASP.Net MVC 4 then it comes up with complete hello world sample MVC web application with default database in the project folder. So, when you want to use your database that already in SQL Server, ASP.Net MVC Entity Framework(EF) code-first technology always try to create a default database instead of connecting to your specified SQL Server database, and display a message “System.Data.ProviderIncompatibleException was unhandled by user code“!
To solve that issue-
First: Go to Web.config file and then find connectionString to update in following way-
<connectionStrings>
<add name="MyDb" connectionString="Data Source=(local);Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
This is for local SQL Server database with Windows Authentication. If you want other your specific database server then go to the following website to download and then install to use for your project.
Second: Open your model file and add/update in the following way (Here, I am using User Model file)-
public class UserDBContext : DbContext
{
//Add the name of your database connection to the base DbContext class
public UserDBContext(): base("MyDb")
{}
public DbSet Users { get; set; }
}
You need to add in same way for all model files in your project.
Hope you this will help!
Leave a Reply