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()); EdmxWriter.WriteEdmx(dbContext,xmlWriter);
After that an EDMX-file is generated to the specified output-location which can then be used as input file for T4 templates.