我使用代码优先来设计模型,并选择 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/