0

I have a very small WPF executable that I want to be stand-alone. I've wittled the external dependencies down to a single MyApp.exe.config file. It needs this because I am using Entity Framework. Is there anyway I can configure this in code so that it can be compiled into my executable?

Here is my config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <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.1" />
    </startup>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
</configuration>
Jordan
  • 9,642
  • 10
  • 71
  • 141
  • 1
    I agree and have cast a re-open vote on this. – John Koerner Sep 18 '14 at 15:21
  • 1
    If you are using EF6, This article has ifnormation on Code-Based configuration: http://msdn.microsoft.com/en-us/data/jj680699 – John Koerner Sep 18 '14 at 15:23
  • Thank you! I am so tired of these zealots. I searched stack overflow before asking my question. They close my question without even reading it or giving an explanation. Stack overflow is not a place to find answers, any more. – Jordan Sep 18 '14 at 15:25
  • 2
    @Jordan: My bad. I must say I find your post very confusing. You are referring to 'stand alone' and the list of assemblies you have. I agreed on the question being duplicate. – Patrick Hofman Sep 18 '14 at 15:27
  • 1
    I agree with patrick, if you are not tying to merge assemblies, what are you trying to do? Are asking how to get the information set up in the App.Config to be configured via code instead of in a external xml file? – Scott Chamberlain Sep 18 '14 at 15:28
  • I am trying to make my executable stand-alone. But my question was about my App.Config file. Not about assemblies. I think @JohnKoerner answered my question, but I'm not sure. – Jordan Sep 18 '14 at 15:28
  • But what do you mean then? The app.config is in the application right? What do you want more? – Patrick Hofman Sep 18 '14 at 15:29
  • @Jordan: Tip for the next time something like this happens, just ping me using `@username`. – Patrick Hofman Sep 18 '14 at 15:30
  • First, I am sorry for my outburst. It just makes me furious that more times than not, my question gets posted as duplicate when it has nothing to do with the supposed duplicate. It has happened too often. I don't want to have an external App.Config file. I want to configure Entity Framework from within my code. – Jordan Sep 18 '14 at 15:31
  • @Jordan: Then just post that part of the app.config. All that other code makes it confusing. Also your text isn't as clear as your last comment. – Patrick Hofman Sep 18 '14 at 15:34
  • 2
    But what do you mean by *stand-alone*? – Sheridan Sep 18 '14 at 15:34
  • A single executable file. I've already worked out the external dll dependencies. I just needed to figure out how to remove the need for an external App.config. – Jordan Sep 18 '14 at 15:38
  • I see what is going on. People are reading my header only. I'll have to change it. – Jordan Sep 18 '14 at 15:45

2 Answers2

3

The link provided in the comments by John Koerner solved my problem. For anyone who is looking for the solution here is the code that I used:

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        SetDefaultConnectionFactory(new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"));
        SetProviderServices("System.Data.SqlClient", SqlProviderServices.Instance);
        SetProviderServices("System.Data.SqlServerCe.4.0", SqlCeProviderServices.Instance);
    }
}

I just included this object in the same assembly as my DbContext. It worked wonderfully.

Jordan
  • 9,642
  • 10
  • 71
  • 141
0

You cannot configure this in code, but you can you ILMerge to merge the EF DLL (and any others you might need) into your exe. Here is an example of how to use ILMerge

Zeus82
  • 6,065
  • 9
  • 53
  • 77