2

Here is my code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(options =>
    { 
     options.UseSqlServer(Configuration.GetConnectionString("AppDbContext"));
    });

    services.AddMvc();
}

And this is my DbContext:

public class AppDbContext : DbContext
{       
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    public DbSet<Actor> Actors { get; set; }
    public DbSet<Movie> Movies { get; set; }
    public DbSet<MovieActor> MovieActors { get; set; }

}

And my ConnectionString is just fine. I'm really wondering why my DB is not generating when I run this code? The breakpoint is hitting in the line services.AddDbContext but when I put a breakpoint in AppDbContext it is not hitting. Can anybody help? My code exactly is looks like this https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro

5
  • did you add migration? Commented Apr 8, 2018 at 19:16
  • @IrakliGabisonia No. I haven't use any migration in my project. Commented Apr 8, 2018 at 19:17
  • see it -> learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db Commented Apr 8, 2018 at 19:18
  • 1
    The call to AddDbContext will not invoke your AppDbContext constructor: That gets invoked when it is first requested from the DI system. You'll need to have a controller/action that takes it as a dependency and then trigger that action to run using the usual MVC stuff. Commented Apr 8, 2018 at 19:27
  • Possible duplicate of Why am I getting an error in my ASP.NET Core SQL Server Express Connection String? Commented Apr 8, 2018 at 19:28

2 Answers 2

7

This create your Db for first time (code at below). However, migration is different than creation. You can check link ( https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx ). If you want to manage Db from your code, DbMigrations scripts execution should be involved your deployment pipeline, otherwise it is not much usefull.

Automatic migration (auto detect changes and executed on db) are also exist (Entity Framework, Automatic apply Migrations). But this does not gives much flexibly and I do not recommend for production.

public class AppDbContext : DbContext
{       
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
        this.Database.EnsureCreated();
    }

public DbSet<Actor> Actors { get; set; }
..............
Sign up to request clarification or add additional context in comments.

1 Comment

this.Database.EnsureCreated(); this is useful if you decided that no more modification in database schema in the future or you can say this command is useful to ensure the database created at the first time.
1

you can use migrations to create a database.

Open the PMC:

Tools –> NuGet Package Manager –> Package Manager Console

Run Add-Migration InitialCreate to scaffold a migration to create the initial set of tables for your model. If you receive an error stating The term 'add-migration' is not recognized as the name of a cmdlet, close and reopen Visual Studio.

Run Update-Database to apply the new migration to the database. This command creates the database before applying migrations.

see it

3 Comments

So you are saying that ef core is not generating the DB automatically without migration like ef standard?
as i know u also can do it : using (var client = new AppDbContext()) { client.Database.EnsureCreated(); }

Your Answer

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