使用Guid作为PK与EF4代码第一 [英] using Guid as PK with EF4 Code First

查看:172
本文介绍了使用Guid作为PK与EF4代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类和表:

public class Foo
{
public Guid Id {get;set;}
public string Name {get;set;}   
}



< hr>


create table Foo
(
id uniqueidentifier primary KEY DEFAULT (newsequentialid()),
name nvarchar(255)
)

问题是,当我尝试保存新的foo第一个是与0000-000-00 ... id和第二个,所以我得到约束异常

the problem is that when i try to save new foo the first one goes with the 0000-000-00 ... id and the second also, so I get constraint exception

任何人都知道修复?

推荐答案

您是否设置了Identity StoreGeneratedPattern?

您可以在 OnModelCreating 方法:

Have you set Identity StoreGeneratedPattern?
You can do it in the OnModelCreating method:

modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

或使用DataAnnotation属性:

or using the DataAnnotation attributes:

public class Foo {
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid Id {get;set;}
  public string Name {get;set;}
}

这篇关于使用Guid作为PK与EF4代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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