New Posts New Posts RSS Feed: Error adding to manager
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Error adding to manager

 Post Reply Post Reply
Author
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Topic: Error adding to manager
    Posted: 14-Nov-2011 at 2:24pm
I'm getting the following error while trying to add an entity to the manager:

Null or pending entities cannot be modified

This is the layout of the data:

User
  UserId

UserProduct
  UserProductId
  UserId
  ProductId

Product
  ProductId

This is the code I am using that fails:

        public void AddProduct(Product product)
        {
            var userProduct = new UserProduct
                                  {
                                      UserId = UserId,
ProductId = product.ProductId                                    };             EntityAspect.EntityManager.AddEntity(userProduct);         }

I have tried it with or without setting ProductId = product.ProductId

User.EntityAspect.IsNullOrPendingEntity = false
User.UserProducts IsPendingEntityList = false

What is very interesting is, sometimes if I set breakpoint on the AddEntity line, wait a few seconds, then let it execute, it works.

The detached entity is trying to resolve something and it is failing initially.

This is the stacktrace:

at IdeaBlade.EntityModel.EntityAspect.SetValueWithChangeNotification(DataEntityProperty property, Object newValue)
at IdeaBlade.EntityModel.EntityManager.GenerateId(Object entity, DataEntityProperty entityProperty)
at IdeaBlade.EntityModel.EntityManager.UpdatePkIfNeeded(EntityAspect aspect)
at IdeaBlade.EntityModel.EntityManager.AttachEntityAspect(EntityAspect aspect, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.<>c__DisplayClass60.<AddOrAttachRelatedEntities>b__5d(EntityAspect w)
at IdeaBlade.Core.EnumerableFns.ForEach[T](IEnumerable`1 items, Action`1 action)
at IdeaBlade.EntityModel.EntityManager.AddOrAttachRelatedEntities(EntityAspect wrapper, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AttachEntityAspect(EntityAspect aspect, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AttachEntity(Object entity, EntityState entityState)
at IdeaBlade.EntityModel.EntityManager.AddEntity(Object entity)
at Spindlemedia.Models.Security.User.AddProduct(Product product)

At the time of the exception:

UserProduct is successfully added to the manager, with a generated id of -100 and it is in the Added State

UserProduct.Product.EntityAspect.IsNullOrPendingEntity = true
UserProduct.User.EntityAspect.IsNullOrPendingEntity = false

They are both in the same entity manager, so I'm not sure what the issue is.

Thoughts?



Edited by smi-mark - 14-Nov-2011 at 2:27pm
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 14-Nov-2011 at 2:34pm
I have found what is causing it.

In my ViewModel I have a collection changed event listening for UserProducts to change. This event is fired before the product has been set on UserProduct.  When I comment the collection changed code, it works flawlessly.

When this collection changes I have the following code:

   private void UserProductsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
  NotifyOfPropertyChange(() => UserProducts);
}

        public IEnumerable<Product> UserProducts
        {
            get { return User.UserProducts.Select(up => up.Product); }
        }


This appears to be the solution:

        public void AddProduct(Product product)
        {
            var userProduct = new UserProduct
                                  {
                                      ProductId = product.ProductId
                                  };
            EntityAspect.EntityManager.AddEntity(userProduct);
 
            userProduct.UserId = UserId;
        }

This way the product is getting resolved before I attach it to the user (Which fires off the collection event)





Edited by smi-mark - 14-Nov-2011 at 2:37pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down