How to connect and use SQL Server 2008 database in ASP.Net MVC 4 EF code-first architecture

This is an important issue that sometimes beginner have faced when they start work with mentioned architecture.

I am going to discussed a very easy solution about this issue-

After create a project in ASP.Net MVC 4 then its comes up with complete hello world sample MVC web application with default database in 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 connectionString and update this entry by following way

<add name="MyDb" connectionString="Data Source=(local);Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient"/>

This is for local SQL Server database with Windows Authentication. If you want other your specific database server then go to following website and find your suitable one.

Second: Open your model file and add/update 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 that way for all model files in your project.

Hope you will find a better way.