使用数据库生成的GUID和datetime与EF4 [英] Using database generated GUID and datetime with EF4

查看:177
本文介绍了使用数据库生成的GUID和datetime与EF4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表格,一个ID(一个 uniqueidentifier ),一个 datetime 和一个值。 / p>

我想在数据库中使用 getdate()进行记录插入时间和 newid()为id。如何配置实体框架来做到这一点?当我尝试在DB上分配ID时,我得到:

 违反PRIMARY KEY约束' PK_Random。无法在对象'dbo.Random'中插入重复键。重复键值为(00000000-0000-0000-0000-000000000000)。 


解决方案

请将以下内容添加到您的guid中。

  [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
public Guid guId {get;组; }

这将启用sql server side guid生成。希望有帮助。


I have a simple table with an ID (a uniqueidentifier), a datetime, and a value.

I'd like to use getdate() on the database for the record insertion time and newid() for the id. How do I configure entity framework to do this? When I try to assign the id on the DB I get:

Violation of PRIMARY KEY constraint 'PK_Random'. Cannot insert duplicate key in object 'dbo.Random'. The duplicate key value is (00000000-0000-0000-0000-000000000000).

解决方案

if you are using code first. Please add the following to your guid.

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid guId { get; set; }

This will enable sql server side guid generation. Hope it helps.

这篇关于使用数据库生成的GUID和datetime与EF4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆