【问题标题】:MS Test integration tests with SQL Server CE and EF 6 migrations使用 SQL Server CE 和 EF 6 迁移进行 MS 测试集成测试
【发布时间】:2016-10-12 06:37:06
【问题描述】:

我想将 SQL Server CE 4 与 EF 6 迁移一起用于我的集成测试中的数据库操作。

实体的 id 是 Guid 的,其中一些是由服务器通过使用生成的:

.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

其他人使用

.HasKey(x => x.Id); 

这也是一个 Guid,但是在持久化之前在实体上设置的东西。

但是,当我的上下文调用时,我得到以下异常: context.saveChanges();

例外:

{"Server-generated keys are only supported for identity columns. The column 'Id' has type 'SqlServerCe.uniqueidentifier', which is not a valid type for an identity column."}

这是因为 sql server ce 无法生成 id 吗?

我对 SQL Server 使用相同的上下文,它工作得很好。

【问题讨论】:

    标签: entity-framework-6 sql-server-ce mstest entity-framework-migrations


    【解决方案1】:

    是的,由于 SQL Server Compact 引擎的限制,无法从引擎中取回生成的 id 值。

    可以设置默认值

    newid()
    

    但引擎无法在保存后获取分配的 Guid 值(与 int 值不同,您可以在其中使用 @@IDENTITY)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多