当我尝试保存新记录或更新现有记录时,EF返回0000-0000-0000-xxx作为Guid? [英] EF returns 0000-0000-0000-xxx as Guid when I try save a new record or update existing record?

查看:51
本文介绍了当我尝试保存新记录或更新现有记录时,EF返回0000-0000-0000-xxx作为Guid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#项目中使用EF4.我面临的问题是,当我尝试保存记录时,出现主键冲突并且PK值为"0000-0000-0000-xxx".根据我的猜测,EF无法识别IsIdentity标志并生成Guid值.在我的表的SQL Server数据库中,我指定了PK(uniqueidentifier> guid)并将其也设置为IdentityColumn,因此,由于guid是在SQL Server中生成的,因此我希望它可以传递到我的项目中.有办法解决这个错误吗?

I am using EF4 in my C# project. The problem I'm faced with is that, when I try to save a record, I get a Primary Key Violation and the PK value is "0000-0000-0000-xxx". From my guess, EF does not recognize the IsIdentity flag and generate a guid value. In my SQL Server database for my table, I specify a PK(uniqueidentifier>guid) and also set it as the IdentityColumn, thus I would expect this to travel through to my project as such since the guid is generated in SQL Server. Is there a way to overcome this bug?

推荐答案

您必须在EF模型中指定 Guid ID作为身份.在EF 4.1中:

You must specify your Guid ID as an Identity in your EF model. In EF 4.1:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }

(与 int ID相比,默认情况下, Guid ID不是身份.)如果使用此模型创建数据库,EF将创建一列,其默认值为 newid().

(Guid Ids are not identities by default, in contrast to int Ids.) If you create the database with this model EF will create a column with a default value of newid().

在EF 4.0中,您可以转到模型设计器,在设计器中标记您的Id属性,打开属性窗口,然后将 StoreGeneratedPattern 设置为 Identity .

In EF 4.0 you can go to the model designer, mark your Id property in the designer, open the properties window and then set StoreGeneratedPattern to Identity.

这篇关于当我尝试保存新记录或更新现有记录时,EF返回0000-0000-0000-xxx作为Guid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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