locked
Avoid entity framework replacing properties added by me RRS feed

  • Question

  • Hi,

    Whenever there are any changes in table column the whole file generated by entity framework replaces all the code with auto generated one. Now I'm using partial class to avoid this problem, But now I have come across a situation where some list has to be initialized in constructor since there are no partial constructor this is not possible, Please suggest any work around to avoid code replace or to do it in a better way.

    • Moved by CoolDadTx Tuesday, December 13, 2016 12:11 AM EF related
    Saturday, December 10, 2016 4:01 PM

Answers

  • but if overloaded constructors are used we need to invoke it manually.

    Go to the DTO man the DTO.... You don't mess with the EF entity if you can help it.

    That's why there solutions like the like one below that you can partial class it, constructor overload it or whatever, leaving the EF entity alone.

    https://entitiestodtos.codeplex.com/

    Sunday, December 11, 2016 10:30 AM

All replies

  • Hi,

    Whenever there are any changes in table column the whole file generated by entity framework replaces all the code with auto generated one. Now I'm using partial class to avoid this problem, But now I have come across a situation where some list has to be initialized in constructor since there are no partial constructor this is not possible, Please suggest any work around to avoid code replace or to do it in a better way.

    You can use EF Code First where you make the classes, unlike EF DB first that makes the classes for you.

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Saturday, December 10, 2016 7:40 PM
  • Hi friend,

    You can to create "overloaded constructors." in your partial class, ok!

    I hope to have helped!


    Saturday, December 10, 2016 8:52 PM
  • but if overloaded constructors are used we need to invoke it manually.
    • Edited by MC Shijith Sunday, December 11, 2016 5:32 AM
    Sunday, December 11, 2016 5:26 AM
  • but if overloaded constructors are used we need to invoke it manually.

    Go to the DTO man the DTO.... You don't mess with the EF entity if you can help it.

    That's why there solutions like the like one below that you can partial class it, constructor overload it or whatever, leaving the EF entity alone.

    https://entitiestodtos.codeplex.com/

    Sunday, December 11, 2016 10:30 AM
  • Perfect friend.

    That's right!

    Sunday, December 11, 2016 1:14 PM
  • Hi MC Shijith,

    Agree DA924x said, I think that Dtos are goods idea, you could initialize the list on Dto constructor, the following blog provide a sample for your reference.

    https://weblogs.asp.net/cibrax/code-generation-with-t4-an-entities-to-dto-example

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 13, 2016 1:45 AM