0

I have the need to regenerate my Entity Model using code. I don't wan't to go and right click and update model from database every time there is a change.

So i started looking at EdmGen and EdmGen2

I use EdmGen2 to pregenerate my Model Views for me:

using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
    process.StartInfo.FileName = @"C:\EdmGen2.exe";
    process.StartInfo.Arguments = @"/ViewGen cs ""C:\Project\EntityFramework\Model\ApplicationEntityModel.edmx""";
    process.StartInfo.WorkingDirectory = @"C:\Project\EntityFramework\Model";
    process.Start();
    process.WaitForExit();
}

This works perfectly.

No i am trying to regenerate my Entity Model:

using (System.Diagnostics.Process process = new System.Diagnostics.Process())
{
    process.StartInfo.FileName = @"C:\EdmGen2.exe";
    process.StartInfo.Arguments = String.Format(@"/ModelGen ""{0}"" ""System.Data.SqlClient"" ""ApplicationEntityModel""", ConnectionString);
    process.StartInfo.WorkingDirectory = @"C:\Project\EntityFramework\Model";
    process.Start();
    process.WaitForExit();
}

This only generates the .edmx file. Can't specify namespaces so this is not working for me.

Is there any tool or template that i can use to completely regenerate my Entity Model', this includes the.edmxanddesigner.cs`?

2 Answers 2

2

I got the source code for the EntityStoreSchemaGenerator and was able to code it up.

You can add these filters and such to limit which tables you want. There is also this crazy pluralizer thing to figure out when to add/remove an "S" from the end of your entity collections. It was failing for the case of a word that ended in Status, it though the singular was Statu (I always laughed at that), but i was able to tell it that the plural of Status is Statuses (even if that is not officially correct) and that the singular of Statuses is Status.

The code is here: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/30b10ed3-b705-458d-ae1f-19d595bceb39/

I found the code to a class that would merge all the files into an edmx. http://fusioncrm.googlecode.com/svn-history/r5/trunk/Fusion.Data.Generation/ModelGenerator.cs

I wrote a consoleapp that runs all this for a client based on a "gold" database, I also hated running this from the designer because it always was slightly different for some reason. This way seems to always generate the same file and as such is clearer what changed when doing check-ins to version control.

1

That will require you to interact with Visual Studio directly (running it on background and command it to do some actions) because auto generation of classes is handled by Visual Studio's custom tool which must run inside visual studio.

Also this automatic generation is quite problematic because there may be changes which requires manual fixing to make EDMX work. They may be also changes which will change your generated classes in the way that the dependent code will not build.

2
  • Thanks for the response Ladislav. So are you saying that i should rather just right click and update model from database or is it possible to interact wit VS and regenerate my Entity Model?
    – Willem
    Aug 24, 2012 at 8:04
  • It is in general possible to interact with VS - there are plenty of COM objects you can call from .NET code but I will not tell you how. I'm even not sure if you can call those methods from outside of visual studio. Every time I need to do something like that I spend ages by searching on web for different pieces of information to build at least partially working solution. Aug 24, 2012 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.