Create EDMX-file while applying Code First

29 Mar

Code first is a very nice feature, especially when you use the migration feature, but what if you are applying the MVVM pattern and want to generate view models from your code first model classes? This can be achieved with T4 templates. But a model generating T4 template, such as that one that generates POCO-classes from an edmx file, requires an edmx file. But when you are using code first this file is, by design, missing. So we need a way to generate an edmx file. This can be achieved with a single static method provided by the Entity Framework. The method of interest is EdmxWriter.WriteEdmx. You can use this method as follows:

var dbContext = new MyDbContext();
var xmlWriter = new XmlTextWriter("OUTPUT-PATH", new UTF8Encoding());

After that an EDMX-file is generated to the specified output-location which can then be used as input file for T4 templates.

Leave a comment

Posted by on March 29, 2013 in EF


Tags: , ,

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: