Print Page | Close Window

Null Entity Incorrectly Attached

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=2929
Printed Date: 13-May-2026 at 9:25pm


Topic: Null Entity Incorrectly Attached
Posted By: Didsbo
Subject: Null Entity Incorrectly Attached
Date Posted: 26-Aug-2011 at 1:53am
Hi,
I'm experiencing unexpected behaviour in my silverlight application. When I add an entity of type Order to the entity manager using EntityManager.AddEntity(order), an entity is also added to the entity manager for the Customer navigation property (order.Customer). The customer entity is effectively a null entity (Id is an empty Guid) however customer.EntityAspect.IsNullEntity is false. The customer null entity should not be added to the manager and should remain detached.
In my WPF application, using the the same entitymodel, the customer null entity is correctly never added to the entitymanager and remains detached when I add an order.
Is this behaviour for Silverlight expected? Why is it different to the WPF behaviour? What can I do to prevent null entities being added to the EntityManager?
Thanks
Ian



Replies:
Posted By: Didsbo
Date Posted: 26-Aug-2011 at 2:08am
Interestingly, if I query EntityManager.Customers before I create and add an order then the customer null entity is correctly not added to the entitymanager.  Wierd.


Posted By: sbelini
Date Posted: 29-Aug-2011 at 11:28am
Hi Didsbo,
 
I was not able to reproduce the behavior you described.
 
  var mgr = new NorthwindIBEntities();
  var order = new Order();mgr.AddEntity(order);
  var customerEntityState = order.Customer.EntityAspect.EntityState; // customerEntityState is Detached
  var isNullCustomer = order.Customer.EntityAspect.IsNullEntity; // isNullCustomer is true
 
Can you provide a sample reproducing the issue? (against NorthwindIB, please)
 
Regards,
   Silvio.


Posted By: Didsbo
Date Posted: 31-Aug-2011 at 4:57am
Hi Silvio,
Thanks for the reply. I'll create a sample using Northwind as requested however in the meantime the following information relating to my model might be useful.
I have an entity call Asset that has a 1 to many association with an entity call SystemItem. The SystemItem entity has a recursive (0..1 to many) associated (Parent/ Children).
If I create a SystemItem as follows I can reproduce the behaviour in my Siverlight application but not in my WPF application (identical model):
  var mgr = new MyEntities();
  var systemItem= new SystemItem();
  systemItem.Id = Guid.NewGuid()
  var assetEntityState = systemItem.Asset.EntityAspect.EntityState; // assetEntityState is Detached
  var isNullAsset = systemItem.Asset.EntityAspect.IsNullEntity; // isNullCustomer is false
  var parentEntityState = systemItem.Parent.EntityAspect.EntityState; // assetEntityState is Detached
  var isNullParent = systemItem.Parent.EntityAspect.IsNullEntity; // isNullCustomer is false
  mgr.AddEntity(systemItem);
  var assetEntityState = systemItem.Asset.EntityAspect.EntityState; // assetEntityState is Attached
  var isNullAsset = systemItem.Asset.EntityAspect.IsNullEntity; // isNullCustomer is True
  var parentEntityState = systemItem.Parent.EntityAspect.EntityState; // assetEntityState is Detached
  var isNullParent = systemItem.Parent.EntityAspect.IsNullEntity; // isNullCustomer is false
I have resolved this issue by quering mgr.Assets (local cache only) before creating a SystemItem. In fact, I don't have to query Assets as any query against the EntityManager will resolve the issue. It seems this issue might be related to the initialisation of the EntityManager. This issue effects all entities in model, not just Assets and SystemItems, where the multiplicity is 1.
If you can't reproduce using NorthwindIB then the problem must be with my model. I can't think where the problem might be. Any ideas?
I thought it might be related by my SystemItem buddy class / metadata - a have a NullValueVerifier attribute on the Asset property. I removed this but still had the issue.
Regards
Ian


Posted By: sbelini
Date Posted: 31-Aug-2011 at 7:44am
Hi Ian,
 
Thanks for the clarification.
 
I'll try to repro it here and let you know.
 
Silvio.


Posted By: sbelini
Date Posted: 31-Aug-2011 at 10:39am
Ian,
 
It looks like I will need a reproducible solution from you as I could not repro it here.
 
I've used Orders *_0..1 Employee and Employee1 *_0..1 Employee2 from NorthwindIB to test:
 
 
var mgr = new NorthwindIBEntities();
 
var employee = new Employee();
mgr.AddEntity(employee);
var employee2EntityState = employee.Employee2.EntityAspect.EntityState; // employee2EntityState is Detached
var isNullEmployee2 = employee.Employee2.EntityAspect.IsNullEntity; // isNullEmployee2 is true
 
mgr.Clear();
 
var order = new Order();
mgr.AddEntity(order);
var employeeEntityState = order.Employee.EntityAspect.EntityState; // employeeEntityState is Detached
var isNullEmployee = order.Employee.EntityAspect.IsNullEntity; // isNullEmployee is true
 
 
Regards,
   Silvio.



Print Page | Close Window