Print Page | Close Window

NullEntity in a list

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=2218
Printed Date: 15-Oct-2025 at 3:37am


Topic: NullEntity in a list
Posted By: chuckc
Subject: NullEntity in a list
Date Posted: 06-Oct-2010 at 2:17pm


I am seeing some unexpected behavior regarding NullEntity.  Is this a bug?  Can NullEntities be added to lists as shown, or is that intrinsically not possible?

Thanks!


Contact contact = EntityManager.Contacts.FirstOrDefault();
User nullUser = EntityManager.GetNullEntity<User>();

try
{
   // At this point, contact.Users.Count == 0
   contact.Users.Add(nullUser);
}
catch (Exception e1)
{
   // Excepts with "Null or pending entities cannot be modified" - presumably because adding
   // the nullUser to the list contact.Users is internally trying to set an association key on nullUser.

   // However, the nullUser is actually added to the Users list.
   // contact.Users.Count is now 1

   bool wtf = false;
}

// Due to some odd behavior I was seeing elsewhere, I noticed that subsequent attempts do *not* except!
// Perhaps that is because the nullUser is already in the list?
try
{
   contact.Users.Add(nullUser);
}
catch (Exception e2)
{
   // Does not except on second attempt!
   bool wtf = true;
}

 

 




Replies:
Posted By: sbelini
Date Posted: 07-Oct-2010 at 12:58pm
Chuck,
 
In the situation above the null entity should not be added to the Contact.Users list.
I am filing a bug for this issue.
 
As for the comment where subsequent attempts do not except, it's because you are trying to relate the same nullUser to the contact twice, so it doesn't even try to add the null entity the second time.



Print Page | Close Window