简体   繁体   English

具有实体框架的WPF应用程序,app.config的正确设置

[英]WPF application with Entity Framework, proper settings for app.config

I have a WPF program that uses Entity Framework (DB first approach). 我有一个使用实体框架(数据库优先方法)的WPF程序。 It works fine on my development laptop, but it crashes right at the beginning on the other computers and it's obviously because it some how can not connect to DB. 它在我的开发笔记本电脑上可以正常工作,但是在一开始在其他计算机上崩溃,这显然是因为它有些无法连接到数据库。

But about the very root of the problem, I read many things online, and although I don't have any certain answer but it seems like it has something to do with the connection string and app.config in general. 但是,关于问题的根本原因,我在网上阅读了很多东西,尽管我没有任何确定的答案,但是似乎它与连接字符串和app.config有关。

I use Microsoft SQL Server Management Studio on my development laptop and I've installed Microsoft SQL Server on the test unit. 我在开发笔记本电脑上使用Microsoft SQL Server Management Studio,并且在测试单元上安装了Microsoft SQL Server。

The whole scenario is that I want to publish my application and any user can download and install it easily. 整个场景是我想发布我的应用程序,任何用户都可以轻松下载并安装它。

Here is my app.config : 这是我的app.config

<?xml version="1.0" encoding="utf-8"?>
<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>
    <connectionStrings>
        <add name="AssetManagementDBEntities"
             connectionString="metadata=res://*/Entity.AssetManagementDBModel.csdl|res://*/Entity.AssetManagementDBModel.ssdl|res://*/Entity.AssetManagementDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;initial catalog=AssetManagementDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
             providerName="System.Data.EntityClient"/>
    </connectionStrings>
</configuration>

Also I have to say, I couldn't find the .dll file that csdl , ssdl and msl must be in that! 我还必须说,我找不到csdlssdlmsl必须在其中的.dll文件!

The "Build Action" of the .edmx file is set to EntityDeploy .edmx文件的“生成操作”设置为EntityDeploy

Finally I found the solution my self: 最终,我找到了解决方案:

As explained here , it's not possible with DB-first approach, because it tries to initialize the model each time, and therefore it needs the DB to exist. 正如解释在这里 ,这是不可能的,DB-第一种方法,因为它试图每次初始化模式,因此它需要DB存在。 So I just copied the classes and files that was created based on my DB and use them like code-first approach (the above link explained it very well). 因此,我只是复制了基于数据库创建的类和文件,并像代码优先方法一样使用它们(上面的链接对此进行了很好的说明)。 Note that you should remove the ConnectionString totally, because as it's explained in that link, the DB context takes care of that. 请注意,您应该完全删除ConnectionString ,因为正如该链接中所述,DB上下文会处理该问题。

Note that you have to have SQLEXPRESS or LocalDB on your machine (if you have installed Visual Studio 2010 or 2012 then one of them is already installed). 请注意,您的计算机上必须具有SQLEXPRESS或LocalDB (如果已安装Visual Studio 2010或2012,则已经安装其中之一)。

Now when the program runs for the first time, it creates DB file in C:\\users\\[your_user_name] (there should be 2 files, the main DB file and the log file). 现在,程序首次运行时,它将在C:\\users\\[your_user_name]创建DB文件(应该有2个文件,即主DB文件和log文件)。

Leave a comment if you have questions about details 如果您对详细信息有疑问,请发表评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM