Using Editor with EF6, can I reuse the Model generated?
Using Editor with EF6, can I reuse the Model generated?
pcjackson
Posts: 18Questions: 6Answers: 0
Hi, I'm porting an existing EF6 application to use Editor. I'm reading the documentation, and it says to use a model class. Of course, I already have model classes because of EF6. Can I reuse these Model classes or modify them or create some sort of templated interface to expose only the things the Editor can handle? I just want to reduce the amount of repeated code, if possible.
(Platform is C# 7.2 using MVC.NET 5 and EF6.)
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Honestly, I'm not sure. If Editor can access the properties of the class via reflection then yes it would work. Its not something I've attempted though. Be interested to know how it works out if you try it.
Regards,
Allan
It seems to have trouble with navigation properties.
(Thanks for the help! I'm not accepting your post as an answer in case I actually manage to figure this out eventually.)
Sorry for the double post. I believe I've figured out a very good way to do this. Caution, this is a code dump post.
So, EF6 uses a template file (*.tt) to generate the code. The code responsible for generating the object looks like this:
Where
EntityClassOpening
is:So what I did is I created a second generator for just a stripped down version of the object without complex properties or navigation properties. It looks like:
Where
SimpleEntityClassOpening
is:This might cause naming conflicts if you're fond of the word simple in your table names. You can of course change this to whatever you desire.
Using these "Simple" models, it seems like the .NET library works just fine, at least for retrieving data.
Awesome - thanks!
Allan
Hello, people of the future. I ran into an issue with DateTime fields with the above code. Per this post you have to ensure that they become string fields instead.
So to make this happen using the EF6 generator, you need to add this method to
CodeStringGenerator
and call it instead ofProperty
in the foreach loop.This is kind of hacky and hard-coded. But hopefully the types we're playing with are more or less stable, so it won't be a problem.