8

Recently migrated from EF Core 1.0 to EF Core 2.0, and it runs fine. Today I added a new table (code-first) and added it to my DbContext with:

public virtual DbSet<Foo> Foos { get; set; }

When I run a migration, with my DbContext in a separate project (remember this was all working before), the migration ends, but no migration files were created. This was all working prior to Core 2.0 using:

http://paultechguy.blogspot.com/2017/04/entity-framework-core-migrating-and.html

PM> Add-Migration GradePremade -project Mfc.MfcRepositoryModel -verbose -context MfcDbContext -StartupProject web.xyz
Using project 'class.xyz\Mfc.MfcRepositoryModel'.
Using startup project 'web.xyz'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.deps.json --additionalprobingpath C:\Users\pcarver\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.0.0\tools\netcoreapp2.0\ef.dll" migrations add GradePremade --json --context MfcDbContext --verbose --no-color --prefix-output --assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\Mfc.MfcRepositoryModel.dll --startup-assembly C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0\web.xyz.dll --project-dir C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel --root-namespace Mfc.MfcRepositoryModel
Using assembly 'Mfc.MfcRepositoryModel'.
Using startup assembly 'web.xyz'.
Using application base 'C:\development\xyz\web.xyz\bin\Debug\netcoreapp2.0'.
Using working directory 'C:\development\xyz\web.xyz'.
Using root namespace 'Mfc.MfcRepositoryModel'.
Using project directory 'C:\development\xyz\class.xyz\Mfc.MfcRepositoryModel'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding BuildWebHost method...
No BuildWebHost method was found on type 'xyz.Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Found DbContext 'MfcDbContext'.
Using DbContext factory 'MfcContextFactory'.
Using context 'MfcDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'web.xyz'...
No design-time services were found.

No errors that seem obvious to me, and no migration files created. I've done a Remove-Migration to start clean, but still no workie.

2
  • For grins, I renamed my database and re-ran the Add-Migrations. Exactly same results. Aug 24, 2017 at 1:26
  • Did you find a solution for this? I got the exact same issue.
    – Peter
    Oct 4, 2017 at 6:57

8 Answers 8

7

There is an issue within the .NET Core class libraries. The successful workaround is to put the following in the csproj file of the Model project:

<PropertyGroup>
  <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> 
</PropertyGroup>
2
  • 1
    I tried a few things like removing all the tables and then the database, removing the migrations, etc. But none of it worked. Finally, this answer worked just perfectly.
    – dotcoder
    Jun 15, 2020 at 4:17
  • is this fixed in .Net 5?
    – Mike W
    Oct 4, 2021 at 21:20
3

I had to mark a project as the start-up project.

2

You will need to update your Program class to look like this

    public class Program
{
    public static void Main(string[] args)
    {
        var host = BuildWebHost(args);

        host.Run();
    }

    // Tools will use this to get application services
    public static IWebHost BuildWebHost(string[] args) =>
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
}
3
  • The Program.cs for my web app does have this already. I also have a Program.cs in my separate class library repository. Note I mentioned a link in my original post of steps I followed to get a separate class library with a working repository; this worked fine prior to EF Core 2.0 and VS 2017 15.x. Aug 24, 2017 at 1:20
  • @user2737646 are you using .NetCore 1.x or .NetCore 2.0?
    – Abdi
    Aug 24, 2017 at 1:25
  • I am using .NetCore 2.0, VS 2017 15.3, EF Core 2.0 Aug 24, 2017 at 1:43
2

I had a similar problem but none of the provided answers fixed it. After reviewing my .csproj-file, I found that Visual Studio automatically added the following lines at some point:

<ItemGroup>
  <Compile Remove="Migrations\**" />
  <Content Remove="Migrations\**" />
  <EmbeddedResource Remove="Migrations\**" />
  <None Remove="Migrations\**" />
</ItemGroup>

After removing this ItemGroup, updating my database worked correctly again.

1

Remove all your IDesignTimeDbContextFactory<TContext> implementations from your migrations project and register your DbContext in your ConfigureServices method in your startup class, the same way you would register when running the application.

Then run your migrations commands as usual, this worked for me after hours of headaches.

1
  • i think this one should work...... as i am trying to use sqlite instead of sqlserver Jun 12, 2021 at 21:02
0

Just try to review the migrations ModelSnapshot.cs file, for differences in the name of a table, creations of relationships (at the end of the code).

0

DbContext class should have parametrless constructor and constructor with options. You have to Override OnConfiguring. Inside of it specified provider for exaple:

options.UseSqlServer(_connectionString);

Code sample:

public class AppDbContext : DbContext
{
    private const string _connectionString = "Data Source=....;App=EntityFramework";


    public AppDbContext(DbContextOptions<AppDbContext> options)
        : base(options)
    { }

    public AppDbContext() : base()
    {
        
    }


    public virtual DbSet<MyEntitie> Users { get; set; }


    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        if (options.IsConfigured)
        {
            return;
        }

        options.UseSqlServer(_connectionString);
    }
}

At the end use command line and run this command (type file name of project where is your DbContext file), alternativly you can use nuget package manager console:

dotnet ef migrations -v -p App.csproj add Init
-3

I tried updating the runtime version in the .csproj file to include:

<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>

in the PropertyGroup tag. It worked for me.

See: Russell Seamer's Solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.