Print Page | Close Window

Error adding to manager

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3093
Printed Date: 13-May-2026 at 7:18pm


Topic: Error adding to manager
Posted By: smi-mark
Subject: Error adding to manager
Date 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?




Replies:
Posted By: smi-mark
Date 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)






Print Page | Close Window