Can't add Entity Framework Core migrations to .NET Standard 2.0 project

前端 未结 2 1426
轮回少年
轮回少年 2021-01-15 21:22

I\'ve got a Solution with many projects. One of them (Domain) is a .NET Standard 2.0 project where I made my EF Core DbContext implementation for w

相关标签:
2条回答
  • 2021-01-15 21:34

    Your csproj file for the class library should contain at least this package. Make sure your versions match for all your Microsoft packages. Either 2.1.0 or 2.1.1 (latest at this time)

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
      </ItemGroup>
    
    </Project>
    

    In the Package Manager Console, set your default project set to the class library where you want the migrations. It's a drop down in the top right of the console window. Also, make sure your start-up project is the project that has the database connection info.

    If the start-up project doesn't have an instance of the DbContext you are wanting to scaffold, you will get the error because it doesn't know how to materialize a DbContext. You should have the DbContext registered in your Startup.cs like this:

    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(connectionString));

    Then, run the command Add-Migration InitialCreate. This will scaffold your migration, it should add a folder to your class library.

    After, run Update-Database and it should apply the migration.

    If you are still having issues. Make sure you are running the latest SDK. https://www.microsoft.com/net/download

    More info can be found in the Docs. https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/

    0 讨论(0)
  • 2021-01-15 21:50
    1. Download and install appropriate Runtime and SDK from here - I guess you need .NET Core 2.1.302 at the moment
    2. Microsoft.EntityFrameworkCore.SqlServer.Design is not needed anymore as it's included to SDK. CLI reference in csproj fiels for EntityFrameworkCore is not needed as well.
    3. Make sure you Manage NuGet packages window shows all updated.
    4. Add anywhere in you web project implementation of IDesignTimeDbContextFactory interface - it will be found automatically and used for EF Add-Migration (or dotnet ef... analogues) command in Package Manager Console

      public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory<ActivitiesDbContext>
      {
          public ActivitiesDbContext CreateDbContext(string[] args)
          {
              DbContextOptionsBuilder<ActivitiesDbContext> builder = new DbContextOptionsBuilder<ActivitiesDbContext>();
      
              var context = new ActivitiesDbContext(
                  builder
                  .UseSqlServer("Data Source=(local)\LocalDB;Initial Catalog=DB_name;Integrated Security=True;")
                  .Options);
      
              return context;
          }
      }
      

    To read the connection string from your appsettings config file you could do the following:

    public class DesignTimeActivitiesDbContextFactory : IDesignTimeDbContextFactory<ActivitiesDbContext>
    {
        public ActivitiesDbContext CreateDbContext(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
                .AddJsonFile("appsettings.Development.json", optional: false);
    
            var config = builder.Build();
    
            var optionsBuilder = new DbContextOptionsBuilder<ActivitiesDbContext>()
                .UseSqlServer(config.GetConnectionString("DefaultConnection"));
    
            return new ActivitiesDbContext(optionsBuilder.Options);
        }
    }
    

    NOTE: in the above code factory will use connection string value defined in appsettings.Development.json file. And connection name is DefaultConnection. In other words, this disign time factory is used for the Code First commands like Add-Migration, Update-Database, etc...) and will apply them to the database connection configured for Development environment.

    0 讨论(0)
提交回复
热议问题