Jordan
Jordan

Reputation: 9901

How can configure Entity Framework within my code?

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>

Upvotes: 0

Views: 702

Answers (2)

Jordan
Jordan

Reputation: 9901

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.

Upvotes: 3

Zeus82
Zeus82

Reputation: 6375

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

Upvotes: 0

Related Questions