New Posts New Posts RSS Feed: Code Second Migration Issues
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Code Second Migration Issues

 Post Reply Post Reply Page  <12
Author
murray.bryant View Drop Down
Groupie
Groupie


Joined: 11-Jan-2012
Location: Australia
Posts: 44
Post Options Post Options   Quote murray.bryant Quote  Post ReplyReply Direct Link To This Post Topic: Code Second Migration Issues
    Posted: 07-Feb-2013 at 6:42pm
My next issue is that the entities no longer have Ideablade.EntityModel.Entity as a base class

Which means I am having issues with things like cloning.

I currently use


 public static T Clone<T>([NotNull] this T source, int level = 0) where T : Entity
        {
            if (source == null) throw new ArgumentNullException("source");
            var handledEntities = new Dictionary<Entity, Entity>();
            return source.InternalClone(level, handledEntities);
        }

        private static T InternalClone<T>([NotNull] this T source, int level, IDictionary<Entity, Entity> handledEntities) where T: Entity
        {
            var res = (source as ICloneable).Clone() as T;
            handledEntities[source] = res;
            if (level > 0)
            {
                foreach (var pi in source.GetType().GetProperties().Where(pi => pi.CanRead))
                {
                    if (pi.PropertyType.IsChildOf(typeof(RelatedEntityList<>)))
                    {
                        var sList = pi.GetValue(source, null) as IList;
                        var list = pi.GetValue(res, null) as IList;
                        if (sList != null && list != null)
                        {
                            foreach (Entity item in sList)
                            {
                                var cItem = handledEntities.ContainsKey(item)
                                                ? handledEntities[item]
                                                : item.InternalClone(level - 1, handledEntities);
                                list.Add(cItem);
                            }
                        }
                    }
                    else if (pi.CanWrite && pi.PropertyType.IsSubclassOf(typeof(Entity)))
                    {
                        var sInstance = pi.GetValue(source, null) as Entity;
                        if (sInstance != null)
                        {
                            var clonedEntity = handledEntities.ContainsKey(sInstance)
                                                ? handledEntities[sInstance]
                                                : sInstance.InternalClone(level - 1, handledEntities);
                            pi.SetValue(res, clonedEntity, null);
                        }
                    }
                }
            }
            return res;
        }

Do you have any advice on how I can now hande this?


Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 07-Feb-2013 at 7:20pm
Since you have control over your Code First entity class definitions, one option is to implement ICloneable in code.
 
A back door of a sort is to take advantage of the fact that Code First entities do have an injected EntityAspect, which you normally don't want to expose directly, but instead use something like an "EntityFacts" class as described here - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/code-first-entityaspect#HEntityFacts.  Anyway, EntityAspect.CloneCore() will clone the entity.  There's one problem here, though, in that this clone will have an incorrect EntityState which you'll need to reset.  Here's a rough way to do it:
 
  var clone = EntityAspect.Wrap(anEntity).CloneCore().Entity as IEntity;
  clone.EntityAspect.EntityState = EntityState.Detached;
 
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down