Cannot create database with Code-First

c# entity-framework-6 sql-server-2016-express wpf


I am using Visual Studio 2015, Entity Framework v6, and SQL Server 2016 Express. In the past I created a database using a SqlConnection and SqlCommand and stuff the SQL into a string.

Now, I am teaching myself EF6 on Entity Framework Tutorial. On the simple code-first example (very simple), I literally copy and paste my code but still do not see the database created in SSMS. Neither does my code throw me any error.

Instead of pasting the code, I did a screenshot. I hope someone can point out what I am or the tutorial is missing.

enter image description here

ï¼»EDIT] Following Sampath's suggestion, I end up getting the following error: enter image description here enter image description here

[EDIT - Solved, sort of]

I apply the same code to another machine of same setup and the code works. So I suspect there are some corruption in the SQL Server or perhaps some registry is incorrect. I uninstall EVERY SQL Server version and related tools, delete all folders and files manually, then freshly reinstall SQL Server Express 2016 and tools. Then my code works.

I don't see this as a solution, but if someone can suggest what may have cause this problem I will try to recreate it or post a real solution to it.

10/9/2016 6:57:02 AM

Popular Answer

You have to give the connection string name on the web.config file as shown below.

Context :

public SchoolContext(): base("MySchoolDB")


App.config file

<add name="MySchoolDB" connectionString="Server=localhost; 
Database=YourDBName;Trusted_Connection=True;" providerName="System.Data.SqlClient" />

You can get more details here : Database Initialization

10/8/2016 10:26:45 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow