I's using dotnet core 2 and EF core 2.
I use CLI to generate migration files.
In module which i try to generate Migration file, i use this factory:
`
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory
{
public ChakavakIdentityDbContext CreateDbContext(string[] args)
{
var builder = new DbContextOptionsBuilder();
//Database Migrations must use a relational provider. Microsoft.EntityFrameworkCore.InMemory is not a Relational provider and therefore cannot be use with Migrations.
//builder.UseInMemoryDatabase("EmployeeDB");
//You can point to use SQLExpress on your local machine
var connectionstring = "Server=.;Database=Chakavak1; Trusted_Connection=True;MultipleActiveResultSets=true";
builder.UseSqlServer(connectionstring);
return new ChakavakIdentityDbContext(builder.Options);
}
}
`
And here is project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Chakava.Identity.Core.csproj">
<Project>{745DB177-A077-496A-9FDE-79A320B8BD6B}</Project>
<Name>Chakava.Identity.Core</Name>
</ProjectReference>
<ProjectReference Include="..\Identity\Chakavak.Identity.csproj" />
<ProjectReference Include="..\Store\Chakavak.Identity.Store.csproj">
<Project>{66A9E441-9F5C-4B39-8CDB-028E549C1675}</Project>
<Name>Chakavak.Identity.Store</Name>
</ProjectReference>
<ProjectReference Include="..\Store\Chakavak.Identity.Store.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0"
PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0-preview1-
final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.0-preview1-
final" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.0-preview1-
final" />
</ItemGroup>
</Project>
But when i run dotnet ef migrations add initial --verbose, i run into this message
`
dotnet ef migrations add initial --verbose
Using project 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj'.
Using startup project 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj'.
Writing 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\obj\Chakavak.Identity.EntityFrameworkCore.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\masoud\AppData\LocalTemp\tmpB699.tmp /verbosity:quiet /nologo D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj
Writing 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\obj\Chakavak.Identity.EntityFrameworkCore.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\masoud\AppData\LocalTemp\tmpB959.tmp /verbosity:quiet /nologo D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj
dotnet build D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\Chakavak.Identity.EntityFrameworkCore.csproj /verbosity:quiet /nologo
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.74
dotnet exec --depsfile D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.deps.json --additionalprobingpath C:\Users\masoud.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --fx-version 2.0.0 "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools.dotnet2.0.1\tools\netcoreapp2.0\ef.dll" migrations add initial --assembly D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.dll --startup-assembly D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0\Chakavak.Identity.EntityFrameworkCore.dll --project-dir D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\ --verbose --root-namespace Chakavak.Identity.EntityFrameworkCore
Using assembly 'Chakavak.Identity.EntityFrameworkCore'.
Using startup assembly 'Chakavak.Identity.EntityFrameworkCore'.
Using application base 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\bin\Debug\netcoreapp2.0'.
Using working directory 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore'.
Using root namespace 'Chakavak.Identity.EntityFrameworkCore'.
Using project directory 'D:\Chakavak.DotNetCore\Identity\EntityFrameworkCore\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'ApplicationDbContextFactory'.
Found DbContext 'ChakavakIdentityDbContext'.
Finding application service provider...
Finding BuildWebHost method...
No entry point was found for assembly 'Chakavak.Identity.EntityFrameworkCore'.
No application service provider was found.
Finding DbContext classes in the project...
Using DbContext factory 'ApplicationDbContextFactory'.
Using context 'ChakavakIdentityDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'Chakavak.Identity.EntityFrameworkCore'...
No design-time services were found.
System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypeAnnotations(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList1 entityTypes, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
`
The same problem with EF Core 2.1-preview1-final (but it works with 2.0.1).
Just new empty project
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0-preview1-final" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0-preview1-final" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.0-preview1-final" />
</ItemGroup>
</Project>
Startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore().AddJsonFormatters();
services.AddDbContextPool<SqlDbContext>(options =>
options.UseSqlServer(_configuration.GetConnectionString("database")));
}
SqlDbContext:
public class SqlDbContext : DbContext
{
public SqlDbContext(DbContextOptions<SqlDbContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PopularPrice>().HasKey(p => p.Id);
builder.Entity<PopularPrice>().Property(p => p.Title).IsRequired().HasMaxLength(200);
builder.Entity<PopularPrice>().Property(p => p.Measure).IsRequired().HasMaxLength(40);
}
}
result of >dotnet ef migrations add Initial
:
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'PopularPriceService'...
No design-time services were found.
System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypeAnnotations(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 entityTypes, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The current CSharpHelper cannot scaffold literals of type 'Microsoft.EntityFrameworkCore.Metadata.Internal.DirectConstructorBinding'. Configure your services to use one that can.
@mexahuk61 I think this link can help you:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/1x-2x-upgrade
@masoud-bahrami @mexahuk61 Looks like you are using a mixture of EF Core 2.0 packages and EF Core 2.1 preview1 packages. Was your intention to use 2.0 or 2.1 preview1?
@ajcvickers why do you think I'm using a mixture? Loot at my csprog file: everywhere EF 2.1...
@mexahuk61 Because <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
includes 2.0 packages.
@ajcvickers oh! You are right! AspNetCore.All package already contains EF. Moreover EF 2.1 depends on DependencyInjection >= 2.1.0 and other important packages... it's strange from my point of view :) Thank you
@ajcvickers yes you are right! and I think this is very confusing about EF 2 or EF 2.1
I update all json packages to use EF 2.0.0
It is possible to use EF2.1preview with current stable .net core 2.0? Obviously Package Microsoft.AspNetCore.All 2.1.0-preview1-final is not compatible with netcoreapp2.0
@Liero - You can use EF 2.1 preview package with netcoreapp2.0. Instead of referencing Microsoft.AspNetCore.All package add package for EF core provider package you would like to use.
It worked for me: https://www.youtube.com/watch?v=i_fDTRoMuG0
I add in .csproj
In terminal: dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.qlServer
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
if you are with net core 2 install the versions of nugget 2.0.X, but if you are with net core 2.1 install versions of nugget 2.1.X
that worked for me
I came from dotnetcore 2.0.1 and ef 2.0.1 and upgraded to 2.1 for both. Then I got the error when trying to add a migration. After applying @krlosruiz suggestion and installing Microsoft.EntityFrameworkCore.Design
in the db project, the issue was fixed.
FYI. Adding Microsoft.EntityFrameworkCore.Design to the DB project fixed it for me.
@mattdone01, greatly appreciate it. Adding it worked for me.
I麓m using MySql, and after installing Microsoft.EntityFrameworkCore.Design in the project, following @krlosruiz, the issue was fixed.
Most helpful comment
It worked for me: https://www.youtube.com/watch?v=i_fDTRoMuG0
I add in .csproj
In terminal: dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.qlServer