¿Cómo generar un modelo de entidad usando código?

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 y EdmGen2

yo suelo EdmGen2 to pregenerate my Model Views para mi:

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();
}

Esto funciona perfectamente.

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();
}

Esto solo genera el .edmx file. Can't specify namespaces so this is not working for me.

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

preguntado el 24 de agosto de 12 a las 08:08

2 Respuestas

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.

El código está aquí: 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.

Respondido 24 ago 12, 14:08

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.

Respondido 24 ago 12, 09:08

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

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. - Ladislav Mrnka

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.