carpics
carpics

Reputation: 2282

Microsoft.EntityFrameworkCore.Sqlite (2.0.0 peview1-final) migration is not creating tables

I am new with Entity Framework and having issue with creating tables in DB.

The database is created fine but it doesn't create tables from entities.

Here is my Context:

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

    public MyContext()
    {
    }

    public DbSet<User> Users { get; set; }

}

And here is User entity

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string UsernaName {get; set;}
}

And this is the code which I have in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
        using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetRequiredService<MyContext>();
            context.Database.Migrate();

        }
}

public void ConfigureServices(IServiceCollection services)
{
   services.AddIdentityServiceAuthentication();

   services.AddDbContext<MyContext>(options => options.UseSqlite(Configuration.GetConnectionString("SqlLiteConnection")));

   services.AddMvc();
 }

Migrate create database context.Database.Migrate(); This is clear to me from the documentation, but I just cannot how to get tables created from entities.

Thanks for any help.

And not sure if it's important, my class MyContext is defined in Class Libarry project, not in web project

EDIT:

From @borisdj answer I figured out that I have to run this command from Package manager:

Add-Migration MyFirstMigration -Context MyContext -Project MyProject.Db

But I am getting this error:

No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

As you can see I am not overriding DbContext.OnConfiguring but I am using AddDbContext in Startup.cs as you can see above and have constructor which accepts DbContextOptions<TContext> object but I am still getting this error.

Is it maybe because MyContext is defined in Class Library project and not in Web project (where Startup.cs is)

Upvotes: 1

Views: 274

Answers (1)

borisdj
borisdj

Reputation: 2509

You should first in Package Manager Console run this command:
PM> add-migration Initial
and then:
PM> update-database

If there are multiple DbContext-s in the App then DbContext Name needs to be specified:
PM> dd-migration Initial -Context MyContext -Project MyProject.Db

Regarding other issue with provider, you should have the following elements in your App: appsettings.json

"ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=CoreTemplate;Trusted_Connection=True;MultipleActiveResultSets=true"

Startup.cs

services.AddDbContext<MyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

MyContext.cs

public partial class My Context : DbContext
{
    public CoreTemplateContext(DbContextOptions options) : base(options) { }

Upvotes: 1

Related Questions