5

Entity Framework Core 2.0 has been recently released and i am using it.

I have added the following line in method ConfigurationServices() in file Startup.cs (My connectionstring is fine).

services.AddDbContext<AutoPartsContext>(options => 
options.UseSqlServer(Configuration.GetConnectionString("NetCore")));

and in Configure method,

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,

var a = _context.Users.Count();

It throws following exception

Invalid object name 'Users'. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)

However, if i create table manually, everything works perfectly. I need all tables to be auto created.

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

1 Answer 1

7

You need to use migrations for this, as database initializers that used to exist in pre-Core EF are no longer available.

Please refer to Migrations - EF Core with ASP.NET Core MVC tutorial.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that helped
sigh Why do they insist on taking away things that made our lives easier. Thanks for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.