在WPF中使用EF Code First和SqlCe [英] Using EF Code First and SqlCe in WPF
问题描述
当我尝试从数据库读取数据时,它会给出错误,但是当我检查数据库上下文对象时,我看到它正在尝试创建它在SqlExpress中。
首先,我如何将其设置为使用CE而不是SqlExpress并设置文件位置?
我尝试更改app.config中的连接字符串,但无法使其工作(它没有创建sdf文件),我也不知道如何设置连接字符串路径到AppData文件夹,因为它是在用户文件夹(未固定)。
从未与SqlCe或EF代码一起工作,所以任何帮助都是欢迎的感谢。
提前感谢。
在很多混乱之后让它工作。我的app.config具有以下内容:
<?xml version =1.0encoding =utf-8?> ;
< configuration>
< configSections>
< / configSections>
< connectionStrings>
< add name =Database
connectionString =Data Source = Database.sdf
providerName =System.Data.SqlServerCe.4.0/>
< / connectionStrings>
< 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>
在部署过程中,DbProviderFactory将会有所帮助。它将允许用户使用SQLCE 4而不运行安装程序(提供您提供的本地binares,以及一些信息: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html )。
I'm trying to use EF Code First with my WPF application , the idea is to create a SqlCe Db in AppData/MyApp (if there isn't one) and use it with EF Code First.
At the moment it's giving error when I try to read data off the database it supposed to create , but when I checked the db context object I saw it's trying to create it in SqlExpress.
First of all , how can I set it to work with CE instead of SqlExpress and set the file location?
I tried changing the connection string in app.config but couldn't get it to work (it didn't created sdf file) and also I'm not sure how to set connection string path to AppData folder as it's in User folder (not fixed).
Never worked with SqlCe or EF Code First before , so any help is welcome & appreciated.
Thanks in advance.
I've managed to get it working after a lot of messing around. My app.config has the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Database"
connectionString="Data Source=Database.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<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>
Having the DbProviderFactory in there will help when it comes to deployment too. It will allow users to use SQLCE 4 without running the installer for it (providing you supply the native binares as well, some info here: http://erikej.blogspot.com/2011/02/using-sql-server-compact-40-with.html).
这篇关于在WPF中使用EF Code First和SqlCe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!