locked
Entity framework with POCO entities RRS feed

  • Question

  • I have entity framework working with POCO entities, in my repository i use base.CreateObjectSet<> to create the collections, my question is

    if my POCO entity has a constructor that takes a parameter that i need to provide in my repository how can that be done ? i'v been reflecting and kinda figured out that objects are being created in the ObjectContext.CreateObject<> ? is there a way to alter this ?

     

    Thanks in advance.

    Sunday, August 7, 2011 6:34 PM

Answers

  • Hi,

    You might reconsider using a POCO class with a constructor that gets a parameter.
    When using POCO feature, EF creates runtime proxies which wrap the POCO entity and inject mechanisms like change tracking and lazy loading.
    One of the restrictions for proxies is a default and empty constructor.
    For further reading about the subject go to http://msdn.microsoft.com/en-us/library/dd468057.aspx

    There is no way to alter the CreateObject method (it is not virtual).
    You can try to set the relevant parameter after the initializtion of the entity inside your repository implementation.


    Gil Fink
    • Marked as answer by Ahmed Galal Monday, August 8, 2011 5:29 PM
    Monday, August 8, 2011 5:48 AM

All replies

  • Hi,

    You might reconsider using a POCO class with a constructor that gets a parameter.
    When using POCO feature, EF creates runtime proxies which wrap the POCO entity and inject mechanisms like change tracking and lazy loading.
    One of the restrictions for proxies is a default and empty constructor.
    For further reading about the subject go to http://msdn.microsoft.com/en-us/library/dd468057.aspx

    There is no way to alter the CreateObject method (it is not virtual).
    You can try to set the relevant parameter after the initializtion of the entity inside your repository implementation.


    Gil Fink
    • Marked as answer by Ahmed Galal Monday, August 8, 2011 5:29 PM
    Monday, August 8, 2011 5:48 AM
  • you can referrer to this article for more information:

    http://www.codeproject.com/Articles/615499/Models-POCO-Entity-Framework-and-Data-Patterns

     
    Sunday, August 24, 2014 4:15 AM