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

  • did you add migration? – Irakli Gabisonia Apr 08 '18 at 19:16
  • @IrakliGabisonia No. I haven't use any migration in my project. –  Apr 08 '18 at 19:17
  • see it -> https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db – Irakli Gabisonia Apr 08 '18 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. – Kirk Larkin Apr 08 '18 at 19:27
  • Possible duplicate of [Why am I getting an error in my ASP.NET Core SQL Server Express Connection String?](https://stackoverflow.com/questions/49695987/why-am-i-getting-an-error-in-my-asp-net-core-sql-server-express-connection-strin) – John Nyingi Apr 08 '18 at 19:28

2 Answers2

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; }
..............
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25
  • 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. – Ashish-BeJovial May 14 '21 at 18:32
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

Irakli Gabisonia
  • 806
  • 8
  • 19