What happened to CSharpMigrationsGenerator and SqlServerMigrationsSqlGenerator - entity-framework-core

After upgrading an existing project from EF Core 5 to EF Core 6, some customizations would no longer compile, specifically customizations to the CSharpMigrationsGenerator and SqlServerMigrationsSqlGenerator classes. Although the namespaces are correct, Visual Studio underlines references to them and doesn't seem to know what I'm talking about. Looking at the EF Core source code, the classes are where they should be, so why can't I see them in my project?

This is a side effect of NuGet updating the package reference. The Microsoft.EntityFrameworkCore.Migrations.Design namespace comes from the Microsoft.EntityFrameworkCore.Design package, which wants to install itself like an analyzer.
After updating the reference, your csproj file will look like this:
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
This is incorrect. This is how you reference an analyzer. It makes the code in that package available to Visual Studio, but not to your code. What you want is a plain old package reference like this:
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0" />
Fixing your csproj file will make the classes available to your code again.

Related

Nuget package reference (WMPLib) not recognized in code

I try to add the WMPLib nuget package as a package reference to my project, but in the code the classes could not be found.
This is in my csproj file:
<PackageReference Include="WMPLib">
<Version>1.0.0</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
And in my cs file I have:
using WMPLib;
And this is the error:
error CS0246: The type or namespace name 'WMPLib' could not be found (are you missing a using directive or an assembly reference?)
Here I read that "the issue is due to the new NuGet format reference 'PackageReference' ... the folders and files aren't added".
Does someone know more about this? And is the solution using a package.config instead of a package reference?
Okay, I was able to reproduce your problem and also found some kind of workaround.
Problem
It seems to me that the original WMPLib NuGet package is somehow broken/not really compliant to nowadays NuGet's expectations. I think so because of the following two indicators:
Normally nuget.org shows all the compatible target frameworks of a package, but for WMPLib, it shows only .NET Framework (see here) - but which version? Compare it e. g. with Newtonsoft.Json.
When analyzing the package with either dotPeek or 7-Zip, I found the assemblies inside the lib folder. But usually, they are inside lib\<<TFM>>, e. g. lib\net48.
Workaround
I've extracted the two assemblies AxInterop.WMPLib.dll and Interop.WMPLib.dll using dotPeek and saved them in C:\temp\NewWMPLib\lib\net35.
Then I've created the following file C:\temp\NewWMPLib.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>NewWMPLib</id>
<version>1.0.0</version>
<authors>me</authors>
<owners>me</owners>
<description>Lorem ipsum</description>
</metadata>
</package>
Finally I build a new NuGet package via nuget pack C:\temp\NewWMPLib.nuspec.
When consuming this package via NuGet, the following code now compiles:
using WMPLib;
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
var windowsMediaPlayerClass = new WindowsMediaPlayerClass();
}
}
}
Disclaimer
I'm not a lawyer and I have no clue whether following my workaround is legally safe.

The specified invariant name 'System.Data.sqlClient' wasn't found in the list of registered .NET data providers in an ASP.NET Core Web API

I am developing ASP.NET Core Web API for an ASP.NET MVC project. So, all services are written based on MVC and I should only call them, however, I come across the above error. I have tried the below codes, unfortunately, none of them worked.
I used RegisterFactory in the Configure method of the Startup class to add the invariantName:
DbProviderFactories.RegisterFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance);
I then added the provider to appsetting.json:
"entityFramework": {
"providers": {
"provider": {
"invariantName": "System.Data.SqlClient",
"type": "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"
}
}
}
Some additional information:
The .NET Core version is 2.2
The EF version is 6.0.0
The Microsoft.EntityFrameworkCore.Tools version is 3.1.20
You can restore some EntityFrameworkCorenuget packages to 2.0.0.
It seems to have solved this problem for me.
These are the packages I downgraded:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />

Why cant I add IsTsVectorExpressionIndex to modulebuilder for EF CORE using postgresql?

I am trying to set up full text search in a .net core 3-web app, using the instructions on NPGSQL website https://www.npgsql.org/efcore/mapping/full-text-search.html?tabs=pg12
In the OnModelCreating-method I set it up according to specifications:
modelBuilder.Entity<Clinic>()
.HasIndex(p => new { p.Name, p.SlugUrl })
.IsTsVectorExpressionIndex("english");
But get the error that IndexBuilder' does not contain a definition for 'IsTsVectorExpressionIndex'.
I tried adding a bunch of nuget-packages, but they dont seem to solve the problem. Creating the index without the TSVector works fine.
Any ideas what Im doing wrong? Packages downloaded:
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.0" />
<PackageReference Include="Npgsql" Version="4.1.6" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.8" />
IsTsVectorExpressionIndex was only introduced in version 5.0.0 of the provider, which was just released a couple hours ago.

.net core 2.1 EF Core reverse engineering errors VS 2017

Not having a great time with doing EF Core Reverse Engineering.
Latest error
The method or operation is not implemented.
Scaffold-DbContext "The method or operation is not implemented"
i get the same errors for both of these commands
Running this from Package Manager Console within VS 2017:
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer
and from command prompt:
dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" Microsoft.EntityFrameworkCore.SqlServer
Yes, I do alter them to have both my data source and database.
I get this error
Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.
0.3.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRES
ULT: 0x80131040)
I have gotten this before and after i upgraded to the .net core SDK 2.2.101
FYI, my csproj file looks like this
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>
</Project>
Between installing the EF power Tools and the Latest Core 2.2.x.x SDK I am now able to change my project to use dropdown change to the .net core 2.2 (it was previously only able to do 2.1.
Then with Nuget updates etc.. , it finally works!

System.IO.FileNotFoundException for Microsoft.EntityFrameworkCore

I have a .NET Standard 2.0 project which uses EF Core via the Nuget package Microsoft.EntityFrameworkCore. Below is the relevant PropertyGroup from the *.csproj file:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
</ItemGroup>
The solution builds, but at runtime, I get a System.IO.FileNotFoundException:
Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies.
That assembly is not in the build folder (or any subfolders such as the netstandard2.0 subfolder). It is in %userprofile%\.nuget\ ...
I have tried:
Cleaning / Rebuilding
Clearing the Nuget cache.
Adding
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> to the
*.csproj file.
Removing and re-adding Micrososft.EntityFrameworkCore (and Microsoft.EntityFrameworkCore.Design, which is also used)
Is there some other way of forcing the package to be included in the build folder (like Local Copy)?
Editing the *.csproj files and adding the following to a PropertyGroup seems to have fixed the problem (or at least provided a reasonable workaround):
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>