Print Page | Close Window

NullReferenceException when setting property with foreign key association

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=2241
Printed Date: 27-Jul-2026 at 8:12am


Topic: NullReferenceException when setting property with foreign key association
Posted By: kaftw
Subject: NullReferenceException when setting property with foreign key association
Date Posted: 19-Oct-2010 at 12:30pm
I recently updated my data model because I added a column to a table, and now for some reason, some code that set a property on one of my data model classes that was working before is now throwing an exception.

The column I added was a simple varchar column that allows nulls and has nothing to do with the values I'm trying to set.  The properties that throw the exception are Guids that have foreign key relationships in the database table and thus also have navigation properties that are related to them.  This only occurs when creating a new entity.  The exception is thrown either when setting those Guid properties or when adding the new entity to the manager (I was trying to play with the order in the hopes it would work).

I have tried deleting the class from the designer and re-adding it, but since it's an essential class to the model, a lot of other classes had navigation properties of that type that were broken and not fixed when adding the class back in through an update of the model, so I had to revert back to the old model.

I even tried reverting back to the model from before I added the new column and forced a code gen, and I still have the same issue, which I find even more bizarre.  The stack trace from the exception goes through a whole slew of DevForce method calls, which I've pasted below.  We're using DevForce 6.0.4.  I appreciate any help that can be given.

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  StackTrace:
       at IdeaBlade.EntityModel.EntityReferenceBase.get_PropertyName()
       at IdeaBlade.EntityModel.EntityReferenceBase.set_IsLoaded(Boolean value)
       at IdeaBlade.EntityModel.ListEntityReference`1..ctor(EntityWrapper fromWrapper, EntityRelationLink link)
       at IdeaBlade.EntityModel.ListEntityReference`1.Clone(EntityWrapper fromWrapper)
       at IdeaBlade.EntityModel.EntityReferenceBase.Create(EntityWrapper fromWrapper, EntityRelationLink relationLink)
       at IdeaBlade.EntityModel.EntityWrapper.EntityReferenceManager.Get(EntityRelationLink relationLink)
       at IdeaBlade.EntityModel.EntityWrapper.EntityReferenceManager.<DiscoverRelations>b__39(EntityRelationLink l)
       at IdeaBlade.Core.EnumerableFns.ForEach[T](IEnumerable`1 items, Action`1 action)
       at IdeaBlade.EntityModel.EntityWrapper.EntityReferenceManager.DiscoverRelations()
       at IdeaBlade.EntityModel.EntityWrapper.EntityReferenceManager.get_InternalReferences()
       at IdeaBlade.EntityModel.EntityWrapper.FixupReferences()
       at IdeaBlade.EntityModel.EntityWrapper.TrackChanged(EntityChangedEventArgs e)
       at IdeaBlade.EntityModel.EntityGroup.OnEntityChanged(EntityChangedEventArgs e)
       at IdeaBlade.EntityModel.EntityGroup.AddNewEntity(EntityWrapper wrapper)
       at IdeaBlade.EntityModel.EntityManager.AddEntityWrapper(EntityWrapper wrapper)
       at IdeaBlade.EntityModel.EntityReferenceBase.AttachIfPossible(EntityWrapper newValueWrapper)
       at IdeaBlade.EntityModel.ScalarEntityReference`1.SetEntity(T newValue, Boolean updateFkValues, Boolean updateInverseValues)
       at IdeaBlade.EntityModel.ScalarEntityReference`1.IdeaBlade.EntityModel.IScalarEntityReference.RefreshForFkChange()
       at IdeaBlade.EntityModel.EntityWrapper.SetValueWithChangeNotification(DataEntityProperty property, Object newValue)
       at IdeaBlade.EntityModel.EntityWrapper.IdeaBlade.EntityModel.IStructuralObject.SetValueWithChangeNotification(DataEntityProperty property, Object newValue)
       at IdeaBlade.EntityModel.DataEntityProperty`2.SetValueWithVerification(TInstance instance, TValue newValue)
       at IdeaBlade.EntityModel.DataEntityProperty`2.<.ctor>b__4(DataEntityPropertySetInterceptorArgs`2 args)
       at IdeaBlade.Core.PropertyInterceptor`3.HandleAction(Action`1 action, TArgs args)
       at IdeaBlade.Core.PropertyInterceptor`3.<BuildTargetedCombinedAction>b__3(TArgs args)
       at IdeaBlade.Core.PropertyInterceptor`3.ExecuteActions(TArgs args)
       at IdeaBlade.Core.PropertyInterceptor`3.SetValue(TInstance instance, Object context, TValue value)
       at IdeaBlade.Core.PropertyInterceptor`3.SetValue(TInstance instance, TValue value)
       at IdeaBlade.EntityModel.DataEntityProperty`2.SetValue(TInstance instance, TValue value)
       at AST.ObjectModel.User.set_Rank(Guid value)
       at AST.Common.Utilities.SL.StateManagementUtil.CreateUser(ASTEntityManager ASTEM, String akoUserName, String firstName, String lastName, String niprnetEmail, String siprnetEmail, String phone, String accessRequestNotes, Rank rank, Organization org, String dsn, Action`1 savedCallback)
       at AST.Web.Home.SL.Views.NewUserRegistrationDialog.OKButton_Click(Object sender, RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnKeyUpInternal(Key key)
       at System.Windows.Controls.Primitives.ButtonBase.OnKeyUp(KeyEventArgs e)
       at System.Windows.Controls.Control.OnKeyUp(Control ctrl, EventArgs e)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException: 




Replies:
Posted By: kimj
Date Posted: 20-Oct-2010 at 4:55pm
Still getting the error after reverting your model is bizarre.  Is it possible that code gen didn't actually work, or that your client-side and server-side models or assemblies have somehow gotten out of sync?  I would try re-generating, cleaning and rebuilding everything one more time just to rule out the possibility.
 
We have fixed a number of problems with navigation properties since 6.0.4, although we hadn't seen this specific problem before, so it's hard to say whether an upgrade to the latest release (6.0.6) would help.  It might be worth a try if upgrading isn't too painful for you right now.
 
Other than the above, can you provide some more information on your application:
- Do you have multiple EDMX in the solution (in either one or multiple assemblies)?
- Are there any many-to-many navigation properties on this particular entity? 
- Do any property getters or setters on this entity have other than public access?
- Does this User class implement IPrincipal/IIdentity?
 


Posted By: kaftw
Date Posted: 21-Oct-2010 at 8:19am
I tried re-generating, cleaning, and rebuilding with the older version of the data model, but still had the same issue.  It would probably be too painful to upgrade the app to use this version as we are code-freezing soon, however, if it fixes this issue, which affects a critical feature for release, then it may be necessary.  I could try it locally and see if it fixes the issue.
 
In response to your other questions:
- We have just one edmx.
- There aren't any many-to-many navigation properties on this entity.
- All property getters and setters are public.
- The class doesn't implement any interfaces.


Posted By: kimj
Date Posted: 21-Oct-2010 at 10:38am
OK.  I assume trying to put together a repro solution will be difficult to impossible, so can you send us or post the EDMX, along with a code snippet of the CreateUser logic?  You can send to IdeaBladeSupportTrack, and use "[#9093]" in the subject line.


Posted By: kimj
Date Posted: 22-Oct-2010 at 5:10pm

Prior to release 6.0.5, DevForce required that both sides of an association be present, i.e., the entity for each role needed to have the corresponding NavigationProperty defined.  In this case, the FK_News_User association doesn't have a User.News navigation property, which results in that ugly exception.

The problem was fixed in the 6.0.5 release, or you can work around it in 6.0.4 by adding the property back into the model.


Posted By: kaftw
Date Posted: 26-Oct-2010 at 9:54am
Unfortunately, your post didn't reach us in time and we eventually took the more drastic route of removing the entity from the model and re-adding it, then fixing up all of the navigation properties that depended on it.  This solved the problem.  Thank you for your time and effort in addressing this.
 
I do have to say that I tried to use 6.0.6 to see if this would fix our issue, but I encountered another issue earlier in the application that was likely caused by the User entity being out of whack.  In addition, I was a bit annoyed by the number of breaking changes to the API that necessitated code changes.  It seems odd to update method signatures and remove constructors in a minor release that would cause many who use your product some unnecessary work and annoyance.  It's certainly not encouraging us to upgrade to 6.0.6 any time soon.



Print Page | Close Window