Reputation: 35
We are using EF6 and EF Core in the project at the same time. I have migration which was created by other team member. I want to Update database using next command:
EntityFrameworkCore\Update-Database
But the next error is occured:
EntityFrameworkCore\Update-Database : The module 'EntityFrameworkCore' could not be loaded. For more information, run 'Import-Module EntityFrameworkCore'.
At line:1 char:1
+ EntityFrameworkCore\Update-Database
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (EntityFrameworkCore\Update-Database:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule
It writing, that the EF Core module could not be loaded, but it in the project packages folder, i checked it.
Import-Module EntityFrameworkCore command executing result:
Import-Module : The specified module 'EntityFrameworkCore' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-Module EntityFrameworkCore
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (EntityFrameworkCore:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
I dont understand why i can
t using it now, and why NPM can not fine EF Core module.
This packages in csproj file are mentioned:
<ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.1" />
<PackageReference Include="EntityFramework" Version="6.1.3" />
<PackageReference Include="IdentityServer4" Version="1.5.2" />
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="1.2.1" />
<PackageReference Include="IdentityServer4.AspNetIdentity" Version="1.0.1" />
<PackageReference Include="IdentityServer4.EntityFramework" Version="1.0.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="System.Linq.Dynamic" Version="1.0.7" />
</ItemGroup>
Can somebody tell me, what i am doing wrong? Update-Database without EntityFrameworkCore prefix is recognized. .Net Framework 4.6.2
Upvotes: 2
Views: 5025
Reputation: 2208
I had the same issue and I had to change the way of working. I wrote this recipe to my mates, I hope it helps you:
"
1st.- HOW TO ADD A MIGRATION:
Go to the project folder and type:
dotnet ef migrations add [NameOFYourMigrationGoesHere] -c YourContext
NOTE: Don't forget to add created files to source control, that's not done auto-magically
2nd.- HOW TO UPDATE YOUR DB's: 2.a.- In docker -> You can run the project (entirely with Docker) and the migration would be applied at the first Context usage. note: (It will use the configured connection string for that environment)
2.b.- Your Local DB -> Edit the Connection String hardcoded in ConfigurationContext.OnConfigure and run:
dotnet ef database update --context ConfigurationContext
STEPS TO REPLICATE THIS FROM SCRATCH:
Migration Commands has changed a lot in .Net Core I recommend to visit: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
1st.- You must add these lines to the .csproj:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.1" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
</ItemGroup>
Note: Version might change
2nd.- Create a Context in your project like this:
public class YourContext : DbContext { #region Constructors
public YourContext()
{
}
public YourContext(DbContextOptions options) : base(options)
{
}
#endregion Constructors
#region DbSets
#endregion DbSets
#region OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
// In order to be able to create migrations and update database:
if (!options.IsConfigured)
{
options.UseSqlServer("YourLocalConnectionStringShouldBeHere");
}
base.OnConfiguring(options);
}
#endregion
#region Model Creating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Your Model Crerating Stuff
}
#endregion Model Creating
}
3rd.- Why not to create an "Initial" Migration according to steps above??
Happy coding!
"
I hope it helps you.
Juan
EDIT:
In addition, the real issue here is that there are different EF "flavours". Just go to EF root documentation and see the differences:
https://learn.microsoft.com/en-us/ef/
EF 6 migrations commands: https://dzone.com/articles/ef-migrations-command
EF Core migrations commands: https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
Now my answer seems complete to me. I hope this saved you time guys.
Upvotes: 2