gpt4 book ai didi

entity-framework - 带有 SQL Server Compact 4.0 的 Entity Framework 6

转载 作者:行者123 更新时间:2023-12-04 00:58:37 29 4
gpt4 key购买 nike

我(仍在)尝试学习有关如何通过代码优先方法使用 Entity Framework 的在线视频教程。我的 EDM 验证正常,现在是实际构建数据库的时候了。讲师使用 SQL Server,显然当针对 DbContext 对象执行第一个查询命令 (ToList()) 时,数据库会自动创建。

由于我计划在 future 的应用程序中使用基于 SQL Server Compact 文件的数据库,因此我尝试使用 SQL Server Compact 4.0 重现本教程的示例。我安装了 NuGet 包“EntityFramework.SqlServerCompact”和“Micrososft SQL Server Compact Edition”。显然,这会导致 app.config 中出现一个新的提供者条目。 :

<providers>
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>

但是当我运行第一个查询命令时,我收到错误消息

Migrations is enabled for context 'DataModelContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.



当我跑 Update-Database我收到消息:

No pending explicit migrations.
Applying automatic migration: 201310311356014_AutomaticMigration.
Running Seed method.



并且没有创建数据库,至少程序仍然退出并显示上述错误消息。

EF 不会自动创建 SQL Server CE 数据库吗?如果是这样,我如何将手动创建的数据库与我的应用程序连接?

最佳答案

这个问题可以通过在 <configuration> 的末尾手动添加一个连接字符串到 app.config 文件来解决。 -标签:

  <connectionStrings>
<add name="DataModelContext"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=C:\Users\sblxxx\Documents\Visual Studio 2012\Projects\Pluralsight\EF5\CodeFirst\db\cfdb.sdf"/>
</connectionStrings>

关于entity-framework - 带有 SQL Server Compact 4.0 的 Entity Framework 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709195/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com