locked
Code First and POCO RRS feed

  • Question

  • Hi,

    I have been experimenting with Code First and I have a question which may seem a little odd:

    Is it possible to use Code First with EF V1 style change tracked entities ?

    I am trying to analyse my options for migrating from EF V1 (.Net 3.5 SP1) to EF V4 (.Net 4.0). I want to use Code First so that I no longer need an EDMX which is something of a friction point as we are looking to expand the team and increase development effort and the EDMX is not as scalable as we would like.

    The nature of the application means that a unit of work can contain more or less arbitrary (i.e. not predefined) changes to the object graph that are currently handled by the EF V1 change tracking mechanisms. Also, the existing code base makes references to the EntityObject base class of the EF V1 entities to determine which entities have been added/modified/deleted.

    As an initial experiment, just to see what would happen, I created POCO entity class and a typed DbContext and successfully retrieved the entities from the database. Then I added EntityObject base class to the entity and now the query results in the following exception:

    System.InvalidOperationException: The type 'EF4CodeFirst.MyEntityType' is not a supported entity type.

    I did not expect this to work, but I thought it might indicate something about the level of compatability.

    So my question is, are there any options for Non-POCO entity classes with Code First ?

    Thanks

     

     

    Wednesday, January 12, 2011 4:15 PM

Answers

  • Hi Graham,

    If you make all the properties of your class virtual and use ICollection<T> for any collection navigation properties then EF will create a 'change tracking proxy' at runtime. Basically this means we create a dynamic type that derives from your POCO type and includes all the logic to perform change tracking (just like types that derive from EntityObject). Deriving from EntityObject in your own classes is not something we recommend.

    POCO classes (without proxies) are still change tracked, but there is some performance overhead as EF needs to take a snapshot of the original values and then compare to these whenever it needs to find modified objects.

    ~Rowan

    Wednesday, January 12, 2011 8:25 PM
    Moderator

All replies

  • Hi Graham,

    If you make all the properties of your class virtual and use ICollection<T> for any collection navigation properties then EF will create a 'change tracking proxy' at runtime. Basically this means we create a dynamic type that derives from your POCO type and includes all the logic to perform change tracking (just like types that derive from EntityObject). Deriving from EntityObject in your own classes is not something we recommend.

    POCO classes (without proxies) are still change tracked, but there is some performance overhead as EF needs to take a snapshot of the original values and then compare to these whenever it needs to find modified objects.

    ~Rowan

    Wednesday, January 12, 2011 8:25 PM
    Moderator
  • Hi Rowan,

    thanks for your response.

    If I use 'change tracking proxies', is there a way (api calls ?) to determine the state of the entity and what properties have changed, etc ?

    Many thanks

    Thursday, January 13, 2011 8:19 AM
  • I have found the Entry method on DbContext, that returns a DbEntityEntry.

    DbEntityEntry has the CurrentValues, OriginalValues and EntityState, but doesn't seem to have a GetModifiedProperties method like the ObjectStateEntry class.

    Is there another mechanism or way of getting to the same info as ObjectStateEntry.GetModifiedProperties ?

    Many thanks

    Thursday, January 13, 2011 12:24 PM
  • Ok, this seems to do what I need:

     

          using (var db = new ModelContext())
          {
            var myEntity= db.MyEntities.First();
            var ctx = ((IObjectContextAdapter)db).ObjectContext;
            var stateEntry = ctx.ObjectStateManager.GetObjectStateEntry(myEntity);
          }
    
    
    Thanks
    Thursday, January 13, 2011 12:53 PM