Hope you can help me, for I ran out of ideas. I use Entity Framework 6 in my project that is good tested and worked just perfectly.
Until I decided to merge everythig into one .exe
file. Since then, I've got problems. For this task I decided to use ILMerge
. Installed it as a nuget package and wrote a Ilmerge.CSharp.targets
like this:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
<Output ItemName="AssembliesToMerge" TaskParameter="Include" />
</CreateItem>
<PropertyGroup>
<ReferenceAssemblies>C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5</ReferenceAssemblies>
</PropertyGroup>
<Exec Command=""..\packages\ilmerge.2.14.1208\tools\Ilmerge.exe" /out:@(MainAssembly) "@(IntermediateAssembly)" @(AssembliesToMerge->'"%(FullPath)"', ' ')"/>
<Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>
</Project>
After a successful compilation I run my solution and get a runtime error
An error occurred creating the configuration section handler for entityFramework: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
Any ideas? By the way... Log4Net seems also not working after merging.
Thanks for you help!
In your configuration file (app.config) you most likely have something like this:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<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>
<!-- more stuff here -->
</configuration>
Note that there are multiple lines there which reference types in EntityFramework assembly, for example:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
But after you merged everything into one assembly - no EntityFramework dll can be found any more. You have to fix those references to point to your main assembly instead. Suppose your application .exe is named MergeTest.exe. Then to fix the issue, replace every occurence of EntityFramework reference to MergeTest:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, MergeTest" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, MergeTest">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, MergeTest" />
</providers>
</entityFramework>
<!-- More stuff here -->
</configuration>
Same story with log4net references.