In entity Framework 4.1 database first, there is a constructor in the generated c# class, so where can I do my partial class custom initialisation ?
In entity Framework 4.1 database first, there is a constructor in the generated c#
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As I understand it, you have a file like Model.edmx in your project that doesn’t actually generate any code. Then you have Model.tt, which is what EF 4.1 actually uses to generate the code. And you can modify this Model.tt. So, if you wanted to add a call to partial method
OnInitialized()to each of the generated entities, that is called from their constructors, find the constructor in the code of Model.tt (its first line should look something likepublic <#=code.Escape(entity)#>()), add the call toOnInitialized()somewhere into the constructor and declare the partial method:Regenerate the entities using Run Custom Tool and you’re done. You can now do something like this in your non-generated code:
I don’t know EF 4.1, so it’s possible that there’s a better way.