微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实体框架 – 具有SQL Server Compact 4.0的实体框架6

我(仍然)试图遵循一个在线视频教程,介绍如何使用实体框架与代码优先的方法.就我的EDM验证是否正确,现在是实际构建数据库的时候了.教师使用sql Server,显然当第一个查询命令(ToList())针对DbContext对象时自动创建数据库.

由于我计划在将来的应用程序中使用基于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.

当我运行更新数据库我收到消息:

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

并且不创建数据库,至少程序仍然退出上述错误消息.

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

解决方法

通过在< configuration> -tag的末尾手动添加一个连接字符串到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>

原文地址:https://www.jb51.cc/mssql/81086.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐