11

This question, asked a year ago, is similar: Does the Entity Framework 4 support generators for id values like NHibernate?

But what I'd like to know is if the code first CTP adds support for identity generation strategies. If not, does anyone know a good extension point in EF to implement something similar?

I'm currently working with model classes which use GUID as the identifier. When inserting using EF they retain their Guid.Empty initial values. I know that you can set a default value for the column in the DB to newid() but that defeats the purpose of client-side identity generation.

Is Entity Framework just not mature enough to be used in a distributed, disconnected system?

Community
  • 1
  • 1
joshperry
  • 41,167
  • 16
  • 88
  • 103
  • 4
    EntityFramework 4.0 is a big step forward from 1.0, but it's still another half baked Microsoft implementation. The extensibility points a few to none. – Jeff Mar 11 '11 at 16:07
  • 1
    @Jeff, While I appreciate your opinion (mine is similar, I prefer NH), for this particular project I don't have the leeway to use something else. – joshperry Mar 11 '11 at 16:08
  • Sorry to hear that. I was more responding to your question "Is Entity Framework just not mature enough to be used in a distributed, disconnected system?" My take on the answer is: it's mature enough, if you don't mind a half baked implementation in each part of the distributed system. – Jeff Mar 11 '11 at 18:35
  • One benefit of GUIDs is that the client can assign identifiers rather than waiting until it is persisted to the database. – Brian Low Nov 26 '12 at 23:20

3 Answers3

16

No, Entity framework code-first is still just nice wrapper around EFv4. There are no NHibernate like generators. If you want client side Id generator you will have to override SaveChanges in derived DbContext and implement your own logic of assigning Ids to new entities.

Edit:

Some high level example:

public class Context : DbContext
{
    // Helper for example
    // DO NOT USE IN REAL SCENARIOS!!!
    private static int i = 0; 

    public DbSet<MyEntity> MyEntities { get; private set; }

    public Context()
        : base("connection")
    {
        MyEntities = Set<MyEntity>();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<MyEntity>().HasKey(e => e.Id);
        // Turn off autogeneration in database
        modelBuilder.Entity<MyEntity>()
                    .Property(e => e.Id)
                    .HasDatabaseGeneratedOption(HasDatabaseGeneratedOption.None);

        // Other mapping
    }

    public override int SaveChanges()
    {
        foreach (var entry in ChangeTracker.Entries<MyEntity>()
            .Where(e => e.State == EntityState.Added))
        {
            // Here you have to add some logic to generate Id
            // I'm using just static field
            entry.Entity.Id = ++i;  
        }

        return base.SaveChanges();
    }
}

public class MyEntity
{
    public int Id { get; set; }
    // Other properties
}
jhappoldt
  • 2,406
  • 1
  • 21
  • 24
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
6

No.

Mine Entity Framework 4.1.10715 installed by NuGet. maybe you could use attribute

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id {get;set;}

please see (The full list of annotations supported in EF 4.1 : CTRL+F in page) here.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
dfang
  • 1,366
  • 15
  • 41
-1
[Key]
public string Id { get; set; }

and then use new GUID ToString

Units.Add( new Unit(){Id=Guid.NewGuid().ToString(), Name="123"});
Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46