New Posts New Posts RSS Feed: Null Entity Incorrectly Attached
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Null Entity Incorrectly Attached

 Post Reply Post Reply
Author
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Topic: Null Entity Incorrectly Attached
    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.


Edited by sbelini - 31-Aug-2011 at 10:40am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Didsbo View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Jun-2009
Posts: 4
Post Options Post Options   Quote Didsbo Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Didsbo View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Jun-2009
Posts: 4
Post Options Post Options   Quote Didsbo Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Didsbo View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Jun-2009
Posts: 4
Post Options Post Options   Quote Didsbo Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down