3

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.

KMC
  • 19,548
  • 58
  • 164
  • 253

2 Answers2

2

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

Sampath
  • 63,341
  • 64
  • 307
  • 441
  • with this I ended up in `The Application is in break mode` error. – KMC Oct 08 '16 at 10:00
  • you have to set your `Database` name on your connection string.I have updated my post. – Sampath Oct 08 '16 at 10:03
  • or can you share your connection string details ? – Sampath Oct 08 '16 at 10:03
  • I have put a link.please see that too. – Sampath Oct 08 '16 at 10:11
  • Thanks. But even I have `Database=MySchoolDB` I still receive the same error. This is not a web application, so I have App.config instead. I would leave it blank because the database does not exist yet and the code first supposed to create this database for me without writing anything explicit, as it was mentioned in the tutorial in your link. – KMC Oct 08 '16 at 10:15
  • OK,can you show this on your code ? `Database.SetInitializer(new CreateDatabaseIfNotExists());` where you have put this ? – Sampath Oct 08 '16 at 10:25
  • I left the constructor empty, as `CreateDatabaseIfNotExists` is the default anyway. But I actually also try to put that to DbContext constructor and I get the same error... – KMC Oct 08 '16 at 10:27
  • then can you create a db on the sql server and put that name on the conn and see the result. – Sampath Oct 08 '16 at 10:29
  • you don't need to create tables.just create a db and see the result.we have to try different ways other than the default if you want to have a solution. – Sampath Oct 08 '16 at 10:36
  • If I don't have a table and data, EntityFramework will not generate the database – KMC Oct 08 '16 at 10:53
  • that is the default behaviour. but on your setup it is not working no.so just create a db and put that db name on your conn and see whether ef generates the tables for you or not. – Sampath Oct 08 '16 at 10:56
  • Sort of, please see my question's last edited section. – KMC Oct 09 '16 at 17:12
2

You have to add entity to your database after configuring connection string, then DBContext will create database. Here is a connection string example:

<add name="Name" connectionString="Data Source=.; Initial Catalog=yourdbName; Integrated Security=True; MultipleActiveResultSets=True;"
      providerName="System.Data.SqlClient" />
karel
  • 5,489
  • 46
  • 45
  • 50
Omid
  • 81
  • 1
  • 5