Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet EF update not generating database

Im following a tutorial to make a dotnet 2 rest api, and expecting the dotnet cli to generate a database after the dotnet ef update. But its not happening

I have this context class

 public class DutchContext : DbContext
 {
     public DutchContext(DbContextOptions<DutchContext> options): base(options)
     {
         public DbSet<Product> Products { get; set; }
         public DbSet<Order> Orders { get; set; }
    }
}

And this is how i call the con string at StartUp.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IMailService, NullMailService>();
    // support for real mail service
    services.AddMvc();
    services.AddDbContext<DutchContext>(cfg =>
    {
        cfg.UseSqlServer(_config.GetConnectionString("ConnectionString"));
    });
}

And the config.json that holds the con string

{

  "ConnectionStrings": {
    "ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=EstudoDotnetDB;Integrated Security=true;MultipleActiveResultSets=true"
  }
}

This is the name of my localdb > (localdb)\MSSQLLocalDB

And the terminal output

$ dotnet ef database update
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\dbeze\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.0.1-rtm-125 initialized 'DutchContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT OBJECT_ID(N'__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT OBJECT_ID(N'__EFMigrationsHistory');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [MigrationId], [ProductVersion]
      FROM [__EFMigrationsHistory]
      ORDER BY [MigrationId];
info: Microsoft.EntityFrameworkCore.Migrations[20405]
      No migrations were applied. The database is already up to date.
No migrations were applied. The database is already up to date.
Done.

Any ideas?

Sorry if this is a dumb question, I'm new to dotnet 2.

like image 243
Daniel Bezerra De Menezes Avatar asked Dec 26 '17 23:12

Daniel Bezerra De Menezes


People also ask

Why dotnet ef is not recognized?

Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a . NET program, but dotnet-ef does not exist. * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

How do I update my dot ef tool?

Update the toolsUse dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.

How do ef migrations work?

EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project's source control like any other source file. Once a new migration has been generated, it can be applied to a database in various ways.


1 Answers

I would try to delete the migrations folders and start over. I've run into this problem before and had to delete the ef migrations and start over.

1) Manually delete your DB - wherever it is (I'm assuming you have your connection sorted), or empty it, but easier/safer is to delete it all together - as there is system __MigrationHistory table - you need that removed too.

2) Remove all migration files - which are under Migrations - and named like numbers etc. - remove them all,

3) Rebuild your project containing migrations (and the rest) - and make sure your project is set up (configuration) to build automatically (that sometimes may cause problems - but not likely for you),

4) Run Add-Migration Initial again - then Update-Database

Post on how to delete and start over a EF migration

like image 149
dev8989 Avatar answered Nov 01 '22 02:11

dev8989