EntityFramework 6.1.3 not finding project assembly

0

From a novice Entityframework User.

While debugging my tests my DataLayer.DLL has access to my data models in a separate DataModels.DLL and can new instances of the data model classes from the DataModels.DLL. When I get to the Context in the DataLayer and I use:

Entry(entity).State = EntityState.Added;

where entity is an instance of a model from DataModels.DLL to mark the entity as Added.

I then get the exception: "assembly DataModels publicKeyToken="null" not found exception. - but the assembly DataModels is already loaded and it is a signed DLL

This error usually means it cannot find the file. What I find odd is the DLL is already loaded so why the exception. See detailed exception below:

LOG: This bind starts in default load context. LOG: Using application configuration file: FullpathTo/Tests/bin/Debug/Tests.dll.config

LOG: Using host configuration file:

LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.

LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).

LOG: Attempting download of new URL file:///FullpathTo/Tests/bin/Debug/DataModels.DLL.

WRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN

ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated. HResult=-2146234304

Message=Could not load file or assembly 'DataModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

InnerException:

My DataModel.DLL is signed. The Tests project reference the project that creates the DataModel.DLL. and my DataModel.DLL is not in the GAC. I do not understand why EntityFramework is looking for the PublicKeyToken=null version of the File.

My Tests project contained NUnit Tests I changed these to MSTests just to check this is not just a NUnit issue as I have seen several similar questions about NUnit methods not finding PublicKeyToken=null versions of assemblies.

I have also tried cleaning and rebuilding, which very occasionallly solves the problem but no where near consistently 1 in about 15-20 times.

I have also removed all the default references that the DataModels are not using from the DataModel. That reduces the possibility of any dependencies failing to load. That solved the problem for a few days and then the problem has come back.

I searched for answers to what causes: "The located assembly's manifest definition does not match the assembly"

I understand that the signed version is not the same as an unsigned version but to be honest I do not understand why EF is looking for an unsigned one. They are none in the obvious places and the signed one is already loaded.

I have added a manifest to my DataModels.DLL to ensure the assembly has a manifest to match the assembly too, not that I have ever had to do that in the past it normally just works.

<assemblyIdentity
    name="DataModels.dll"
    version="1.0.0.0"
    publicKeyToken="53ff00ed9cb790e2"
    Culture="neutral"
    processorArchitecture="msil"/>

in the DataModels.DLL.Manifest

and I added Tests.DLL.Manifest with the matching dependency:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity
                name="DataModels.dll"
                version="1.0.0.0"
                publicKeyToken="53ff00ed9cb790e2"
                Culture="neutral"
                processorArchitecture="msil"/>
            <bindingRedirect oldVersion= "1.0.0.0"
                  newVersion="1.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>    

in the Tests.DLL.Manifest

which made absolutely no difference what so ever. I think the manifests are a red herring and I am not 100% sure I got them right

I have tried removing bindingRedirect:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity
                name="DataModels.dll"
                version="1.0.0.0"
                publicKeyToken="53ff00ed9cb790e2"
                Culture="neutral"
                processorArchitecture="msil"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>    

also with no success.

My starting point for investigating this problem already has the DataModels.DLL referenced by the DataLayer.DLL. As I said the DataLayer can new instances of Models successfully so the problem appears to be between EF 6.1.3 and my DataModels.DLL for some reason I do not understand.

Here are my Tests.config EF entries in case there is something wrong there:

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="mssqllocaldb" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS2014;Initial Catalog=DbMame;Trusted_Connection=SSPI;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
c#
entity-framework
dll
asked on Stack Overflow Aug 19, 2016 by Robin • edited Aug 22, 2016 by Robin

1 Answer

0

A bit late to the party. But still, maybe someone will find it useful.

Prerequisites: Entity Framework 6, code first migrations.

I had a similar issue. In my case, the issue was that the assembly, that fails to load properly (DataModels.DLL in your case), was signed after the migration had been created.

During a migration with Entity Framework 6, a record in __MigrationHistory table is created. This record has a Model column. Which is some sort of hash of models used for migration. At the runtime, Entity Framework tries to find the needed assembly using this cache. Signing the assembly after migration changes the full assembly name and this cache becomes invalid.

Adding a new migration, even empty, solves this issue because now __MigrationHistory table has a new record with a correct hash in the Model column.

answered on Stack Overflow Sep 29, 2020 by ArtMalykhin

User contributions licensed under CC BY-SA 3.0