Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

entity framework 4 - using Guid as PK with EF4 Code First

I have this class and table:

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

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

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

anybody knows a fix ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

or using the DataAnnotation attributes:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...