Work with GUID and Entity Framework

This question emphasizes that you cannot use a server-based GUID with an entity infrastructure. But I want the GUID generation to be processed at the DAL level of the database API (i.e., when the object constructor is called, I want the object identifier to be initialized with a new GUID). My plan is to write a small tool to create heaps of code files that are partial entity classes. I have a way to do this, the question is: "Do I really think that I am doing this, or should I do this?

My problem is that when the edmx file is updated, I don’t want to edit a bunch of code files either, I just want to run a tool that will do what is needed.

Again, is the head straight?

+5
source share
1 answer

Well, if you look at the partial classes that the Entity Framework generates by default, there is no default constructor.

So this in a separate partial class will work well:

public partial class Customer{
    public Customer(){
         _ID = Guid.NewGuid();
    }
}

So there is probably no reason not to do what you are planning.

You might want to look into the T4 templates. Thus, EF 4.0 (i.e., EF in .NET 4.0) allows you to customize the generated code. Now, while in 4.0 this experience is completely impossible, you can easily build something based on T4 just to create these partial number classes that will work very well in .NET 3.5 SP1.

Hope this helps

Alex

+6

All Articles