带有Entity Framework的WPF应用程序,适用于app.config的设置

时间:2022-04-12 12:51:42

I have a WPF program that uses Entity Framework (DB first approach). 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.

我有一个使用Entity Framework的WPF程序(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!

另外我不得不说,我找不到csdl,ssdl和msl必须在的.dll文件!

The "Build Action" of the .edmx file is set to EntityDeploy

.edmx文件的“Build Action”设置为EntityDeploy

1 个解决方案

#1


0  

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

如此处所述,使用DB-first方法是不可能的,因为它每次都尝试初始化模型,因此需要DB存在。所以我只是复制了基于我的数据库创建的类和文件,并像代码优先方法一样使用它们(上面的链接很好地解释了它)。请注意,您应该完全删除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文件和日志文件)。

Leave a comment if you have questions about details

如果您对细节有疑问,请发表评论

#1


0  

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

如此处所述,使用DB-first方法是不可能的,因为它每次都尝试初始化模型,因此需要DB存在。所以我只是复制了基于我的数据库创建的类和文件,并像代码优先方法一样使用它们(上面的链接很好地解释了它)。请注意,您应该完全删除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文件和日志文件)。

Leave a comment if you have questions about details

如果您对细节有疑问,请发表评论