I’m using the latest EntityFramework 5 from NuGet. I don’t know if I’m doing wrong, but it seems that complex types don’t work with this template! (???) No matter what I do, I always get this compile-time error:
Running transformation: System.InvalidCastException: Unable to cast object of type ‘System.Data.Metadata.Edm.ComplexType’ to type ‘System.Data.Metadata.Edm.PrimitiveType’.
For example:
-
I created an empty model “SimpleModel”, then I added a simple entity in it, “Foo” and a complex property called “Something” of complex type “Bundle”. Did all this using the EDM designer.
-
I went to “Generate Database from Model” so as to persist the model in a SQLEXPRESS DB. Ran the script and everything went OK. I tested writing some stuff using the new entity model and it worked fine, i.e., SaveChanges() worked OK.
-
I went to “Add Code Generation Item” and selected this template: EF 5.x DbContext Fluent Generator to create the “SimpleModel.tt” series of template files and I immediately got the error above.
When looking at the “SimpleModel.Mapping.cs” file generated by the “SimpleModel.Mapping.tt” template, I see that this code file is incomplete. This is the the only file incomplete, i.e., the generation of mapping code is breaking. Is this template compatible with the latest EF 5?
I have an existing DB with several tables and an entity model in place based on ObjectContext and EntityObjects (i.e, Model-First stuff). Most importantly, I have an existing data layer that makes use of entities mapped to these tables and lots of the data is rather appropriately bundled in complex types (bundled numeric and text stuff with no identity).
The point is that I need to migrate my entity model from using ObjectContext and EntityObjects to DbContext and POCOs, then I want to go Code-First. How should I do this?
Any help is appreciated beforehand!
Imho either use EF Power tools (but I think they don’t support EF 5 yet) and reverse engineer database or write code first mapping yourselves.
That generator is not provided by MS and if you have any problem with it you should report it to its developer as a bug. If you check Q/A for this generator in Visual Studio Gallery it claims that complex types are not supported.