ErikEJ / EntityFramework6PowerTools

This is the codebase for Entity Framework 6 Power Tools Community Edition, and modern EF 6 providers for SQL Server and SQL Server Compact
Other
183 stars 27 forks source link

The Provider Manifest is incorrect #93

Closed jdavi37 closed 2 years ago

jdavi37 commented 2 years ago

Hello!

My team is working on a project which uses Entity Framework 6.4. However due to project requirements we need to migrate to using Microsoft.Data.SqlClient, and so we are using your NuGet package. We removed references to EntityFramework, installed your nuget package, updated our custom DbContext with the DBConfigurationType attribute [DbConfigurationType(typeof(System.Data.Entity.SqlServer.MicrosoftSqlDbConfiguration))] as you mentioned in the readme, and updated all references to System.Data.SqlClient to the new Microsoft.Data.SqlClient.

Some of our team members run into this "Provider Manifest/PrimitiveTypeKind" error at runtime when the dbcontext is used. Any ideas as to what might be causing this issue, or next steps to troubleshoot? It seems these environments are having an issue loading "System.Data.Resources.SqlClient.SqlProviderServices.ProviderManifest.xml."

The offending code looks like:

using (var ctx = new MyCustomDbContext(_connectionString))
{
    var dbEntities = ctx.SomeEntity.Include("SomePath").ToList(); //this line errors    
...

Full Error

(167,34) : error 0005: The 'PrimitiveTypeKind' attribute is invalid - The value 'HierarchyId' is invalid according to its datatype 'http://schemas.microsoft.com/ado/2006/04/edm/providermanifest:TPrimitiveTypeKind' - The Enumeration constraint failed. (167,10) : error 0122: The value 'HierarchyId' is not a valid PrimitiveTypeKind. Source = EntityFramework StackTrace = at System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest.Load(XmlReader reader) at System.Data.Entity.SqlServer.SqlProviderManifest..ctor(String manifestToken) in /_/src/ErikEJ.EntityFramework.SqlServer/SqlProviderManifest.cs:line 62 at System.Data.Entity.SqlServer.MicrosoftSqlProviderServices.<>c.b__29_0(String s) in /_/src/ErikEJ.EntityFramework.SqlServer/MicrosoftSqlProviderServices.cs:line 482 at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path) at System.Data.Entity.Infrastructure.DbQuery`1.Include(String path) Thanks for your time.
CZEMacLeod commented 2 years ago

@jdavi37 To use HierarchyId or the spatial you need to load the sql server types from a custom assembly. Not sure exactly how this works for Microsoft.Data.SqlClient, but I'm guessing you need startup code similar to

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
System.Data.Entity.SqlServer.MicrosoftSqlProviderServices.SqlServerTypesAssemblyName = typeof(SqlGeography).Assembly.FullName;

Note that I have changed the 'normal' System.Data.Entity.SqlServer.SqlProviderServices to System.Data.Entity.SqlServer.MicrosoftSqlProviderServices so that the new provider is set.

Haven't tried this myself (yet) but I may need to in the future as I too am looking to migrate to MDS and use HierarchyIds in my code.

ErikEJ commented 2 years ago

I was not able to repro after following the guide for mapping here: https://softwarehut.com/blog/tech/hierarchyid-entity-framework - in a .NET Framework project with Code First from Database.

jdavi37 commented 2 years ago

We aren't actually using the 'HierarchyId' type anywhere in the project. I have a super simple repro project that I can share (the zip is 84 MB unfortunately, so github won't let me attach it here). The DB has only one table with only one property, an int ID. For most of our developers, this project runs fine. For just a few, they run into the error above. Is there some part of this that is reliant on certain versions of SQL Server or some other obscure dependency that we could check to see if these environments are missing?

EDIT: On one of the affected developer machines, I cloned down the repo and opened the Solution "ErikEJ.EntityFramewrok.SqlServer" in vs 2019. When I open up the "System.Data.Resources.sqlClient.SqlProviderServices.ProviderManifest.xml" file in VS, I see the error here as well: image

ErikEJ commented 2 years ago

Do not understand why this validation error occurs, would like to get a repro project (and information about the SQL Server edition/version in use for the afftected developers)

https://github.com/dotnet/ef6/blob/main/src/EntityFramework/Resources/System/Data/System.Data.Resources.ProviderServices.ProviderManifest.xsd#L122

Maybe @bricelam or @ajcvickers has some clue?

ErikEJ commented 2 years ago

@jdavi37 You can share something and let me know via email: ejlskov at hotmail dot com

jdavi37 commented 2 years ago

Sent the repro project and some details over to you.

ErikEJ commented 2 years ago

Thanks for the repor project, unable to repro on my PC.

For the affected PCs:

Which exact VS version? Which OS version? Which SQL Server version do they access?

Do they have: "Microsoft SQL Server System CLR Types" installed? (check in Add/Remove)

jdavi37 commented 2 years ago

Affected PCs: machine 1 Visual Studio Professional 2019 16.4.2 Windows 10 Enterprise 10.0.18362 SQL Server 2014 12.0.5000.0 has "Microsoft System CLR Types for SQL Server 2014" installed

machine 2 Visual Studio Enterprise 2019 16.11.9 Windows Server 2019 Datacenter, version 1809, OS build 17763.2452 SQL Server 2019 15.0.4.188.2 has "Microsoft System CLR Types for SQL Server 2019 CTP2.2" installed

machine 3 Visual Studio Enterprise 2017 15.9.36 Windows Server 2021 R2 SQL Server 2014 12.0.5000.0 has a number of CLR types for SQL Server installed image

machine 4 Visual Studio Enterprise 2015 (14.0.23107.178), 2017 (15.9.28307.1064) & 2019 (16.6.30204.135) Windows Server 2019 Standard version 2809 17763.2452 SQL Server 2016 14.0.3430.2 Also has a number of CLR Types for SQL Server installed image

ErikEJ commented 2 years ago

@jdavi37 I honestly have no idea...

ErikEJ commented 2 years ago

Oddly enough I see the same error in VS 2022 XML editor:

image

ErikEJ commented 2 years ago

Just a thought: is EntityFramework.dll in GAC?

You can run

gacutil /l > gaclist.txt

and look at the result

jdavi37 commented 2 years ago

It is not in the GAC, according to that command.

ErikEJ commented 2 years ago

I have run out of good ideas atm

jdavi37 commented 2 years ago

So I checked again (not sure how I missed it the first time) and we did have EF 6 dlls in the GAC on these machines, and removing them resolves the issue. Thanks so much!

ErikEJ commented 2 years ago

Hooray!!

And thanks for your patience and help. This is also very valuable for future usage of this library, I will add this info to the docs.