Solved

How to create custom ID generator for Entity Framework?

Posted on 2013-05-11
2
3,096 Views
Last Modified: 2013-05-16
How can I create a custom ID generator for Entity Framework to use a unique string for a primary key?

My client has a requirement that all tables must contain a primary key of a GUID but to be stored as a CHAR(32) without dashes for braces.  Currently, I am having to manually stuff the PK values manually while instantiating entities and/or adding them to collections.  I would really like for Entity Framework to call a small piece of custom logic to create the primary key AND have this generator available in the Designer for model-first design:

id = Guid.NewGuid().Replace("-", "").Replace("{", "").Replace("}", "");

Open in new window

0
Comment
Question by:MrNMrsGeek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39159198
the following posts explained it:
http://stackoverflow.com/questions/5275306/does-entity-framework-4-code-first-have-support-for-identity-generators-like-nhi
http://stackoverflow.com/questions/5643620/assigning-ids-to-entities-with-entityframework-4

basiaclly, u override OnModelCreating and call HasDatabaseGeneratedOption() with HasDatabaseGeneratedOption.None to disable auto ID generation.
then u override SaveChanges() and use your generation code for the identity column:
public override int SaveChanges()
    {
        foreach (var entry in ChangeTracker.Entries<MyEntity>()
            .Where(e => e.State == EntityState.Added))
        {
            entry.Entity.Id = Guid.NewGuid().Replace("-", "").Replace("{", "").Replace("}", "");
        }

        return base.SaveChanges();
    }
}

Open in new window

0
 

Author Closing Comment

by:MrNMrsGeek
ID: 39172313
Not exactly a generator.  Not exactly what I wanted.  However, this works, I guess.  I was specifically looking for a generator.  Overriding SaveChanges is a hack in that I must either (a) force all of my types to have the same property, or (b) check the entity type within SaveChanges to find all custom types that have said property, or (c) use Reflection to determine IF the object has the property.  None of these are what I want.  My only reason for awarding this as a solution was because it is not sedgwick's fault.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question