none
Calculated fields RRS feed

  • Question

  • I have a partial class  created by Entity framework.

    Some of the properties are calculated from other properties.

    I am not allowed to create a partial property. Is there some other way to inject some code that is executed when a property is updated?


    Certified Geek

    Wednesday, May 21, 2014 6:02 PM

Answers

  • That sound to complicated.

    I have added a new method to a partial class. I only had to find a proper place in my code to call that method.


    Certified Geek

    • Marked as answer by Arne MN Wednesday, May 28, 2014 11:50 AM
    Wednesday, May 28, 2014 11:49 AM

All replies

  • Well about the only way I can think of is to do it in the class that Entity Framework created and do it right in the property definition. This way you are using the one that is already there. Of course this means that every time you modify the database and update the model it will overwrite your added code and you will need to recreate them,

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, May 21, 2014 7:24 PM
  • I need to be able to regenerate my model.

    Someone must have run into this problem before. I need to find a better solution.


    Certified Geek

    Wednesday, May 21, 2014 7:47 PM
  • Yeah, it's called delete the model out of the project and regenerate it. :)
    Wednesday, May 21, 2014 10:38 PM
  • And the solution was to create a partial class with partial properties.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by Arne MN Tuesday, May 27, 2014 7:51 PM
    • Unmarked as answer by Arne MN Tuesday, May 27, 2014 7:52 PM
    Thursday, May 22, 2014 1:21 AM
  • A partial class is helpful.

    Partial properties does not exist.


    Certified Geek

    Tuesday, May 27, 2014 7:52 PM
  • Hi Arne;

    Sorry about that you are absolutely correct about partial properties, they do not exist. I do not know what I was thinking, well I was not thinking.

    Well what you can do and this does exist is to modify the T4 templet for this project and add the code needed to create in the classes that Entity Framework created for you the custom logic you need. In other words let Entity Framework do it for you each time you recreate the model. EF uses the files in your application with extension .tt which are T4 templates. Please read through the article T4 Templates and the Entity Framework which will go through this with you. 


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, May 27, 2014 9:01 PM
  • That sound to complicated.

    I have added a new method to a partial class. I only had to find a proper place in my code to call that method.


    Certified Geek

    • Marked as answer by Arne MN Wednesday, May 28, 2014 11:50 AM
    Wednesday, May 28, 2014 11:49 AM