c# - SQL Server CE 4.0、Code First 和 GUIDE key

标签 c# sql-server-ce code-first guid

我使用代码优先来设计模型,并选择 GUID 作为主键的数据类型。我的类(class)如下所示:

public class AccountMovementPlan
{
    [Key, DatabaseGenerated( System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity )]
    public Guid AccountMovementPlanID { get; set; }

    [Required]
    public Guid UserAccountID { get; set; }

    [ForeignKey("UserAccountID")]
    public UserAccount UserAccount { get; set; }     

// etc, etc, etc   
}

接下来,我选择 SQL Server CE 4.0 作为我的数据存储,实现了使用 SqlCEConnectionFactory 的配置并使用 DropCreateDatabaseIfModelChanges 作为初始化方法。

但是,当我尝试运行我的应用程序时,我遇到了错误

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException occurred in EntityFramework.dll

这告诉我,

The identity column must be either an integer or big integer data type and cannot be NULL.

我的问题很明显:代码优先的 SQL Server CE 不支持自动生成的 GUID 作为 PK?或者,我有什么地方出错了吗?

非常感谢。

最佳答案

不,不支持这种情况,您必须在代码中使用 Guid.NewGuid() 来分配值。

关于c# - SQL Server CE 4.0、Code First 和 GUIDE key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403645/

相关文章:

.net - Entity Framework CTP5 代码优先 : Mapping a class with multiple collections of another class

c# - 当锁定持续时间尚未过去时,Azure 服务总线对非分区队列抛出 MessageLockLostException

sql-server-ce - 如何使用 SSMS 连接到 SQL Server CE 文件

c# - SqlCeConnection 处理中的访问冲突异常

c# - 为什么我的 LINQ 插入在 SQL Server CE 3.5 中不持久?

C# ASP.NET MVC 3 SQL 数据库到 html.dropdownlistfor 和 AutoPostBack 到选择和项目的浏览操作

c# - Pechkin HTML 到 PDF,包含指向无扩展名 PNG 的链接

c# - ASP .Net - MySql - 无法读取任何数据

c# - Azure Key Vault "Reference Syntax"不执行任何操作

c# - 如何使 LINQ 到多个表