Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在我的代码中配置实体框架?_C#_Wpf_App Config - Fatal编程技术网

C# 如何在我的代码中配置实体框架?

C# 如何在我的代码中配置实体框架?,c#,wpf,app-config,C#,Wpf,App Config,我有一个非常小的WPF可执行文件,我希望它是独立的。我已经将外部依赖项简化为一个MyApp.exe.config文件。它需要这个,因为我使用的是实体框架。我是否可以在代码中对此进行配置,以便将其编译到可执行文件中 这是我的配置文件: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on

我有一个非常小的WPF可执行文件,我希望它是独立的。我已经将外部依赖项简化为一个MyApp.exe.config文件。它需要这个,因为我使用的是实体框架。我是否可以在代码中对此进行配置,以便将其编译到可执行文件中

这是我的配置文件:

<?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>


您不能在代码中配置此功能,但可以通过ILMerge将EF DLL(以及您可能需要的任何其他DLL)合并到exe中

John Koerner在评论中提供的链接解决了我的问题。对于任何正在寻找解决方案的人,以下是我使用的代码:

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);
    }
}

我只是将这个对象包含在与DbContext相同的程序集中。它运行得非常好。

我同意,并对此进行了重新投票。如果您使用的是EF6,本文将对基于代码的配置进行ifnormalation:@Jordan:My bad。我得说我觉得你的帖子很混乱。您指的是“独立”和您拥有的程序集列表。我同意重复这个问题。我同意patrick的观点,如果你不想合并程序集,你想做什么?你在问如何在App.Config中设置信息,以便通过代码而不是外部xml文件进行配置?但你说的单机版是什么意思?这根本不是我想要的。