Efcore: Entity Framework Core 2 - Code First not creating tables automatically

Created on 24 Aug 2017  路  3Comments  路  Source: dotnet/efcore

I have added the following line in method ConfigurationServices() in file Startup.cs (My connectionstring is fine).
```c#
services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("NetCore")));

and in Configure method, 

```c#
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetRequiredService<MyConntectionStringName>();
                context.Database.Migrate();
                context.Database.EnsureCreated();
            }

This is creating the database but not tables. When i call a simple count on my tables,
```c#
var a = _context.Users.Count();



It throws following exception

Exception message: "Invalid object name 'Users'."
Stack trace: at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
```

Further technical details

EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 Preview 2

closed-question

Most helpful comment

@mohsin91 A few things:

  • If you are using EF Core 2.0 with ASP.NET 2.0. make sure you use the new item template or follow the instructions for updating your application. In particular, EF Core doesn't call ConfigureServices anymore and creating/seeding the database in Configure will cause issues.
  • Migrate() is the way to problematically apply migrations that have already been created. It relies on tooling being used to create the migrations. See https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db for more details
  • EnsureCreated can be used to create the database and tables in prototyping and testing scenarios. It should be used instead of, not together with Migrations. It will only create the database if it does not exist or contains no tables. In other cases it will do nothing.

All 3 comments

@mohsin91 A few things:

  • If you are using EF Core 2.0 with ASP.NET 2.0. make sure you use the new item template or follow the instructions for updating your application. In particular, EF Core doesn't call ConfigureServices anymore and creating/seeding the database in Configure will cause issues.
  • Migrate() is the way to problematically apply migrations that have already been created. It relies on tooling being used to create the migrations. See https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db for more details
  • EnsureCreated can be used to create the database and tables in prototyping and testing scenarios. It should be used instead of, not together with Migrations. It will only create the database if it does not exist or contains no tables. In other cases it will do nothing.

@ajcvickers EnsureCreated does not create the tables for me in the unit tests. How exactly is this supposed to work?

@DominikDitoIvosevic If it's not working, then please open a new issue and include a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

Was this page helpful?
0 / 5 - 0 ratings