Solved

How to create custom ID generator for Entity Framework?

Posted on 2013-05-11
2
2,951 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

685 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