Print Page | Close Window

'Non-native' DataEntityProperties not getting default values initialized

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=3354
Printed Date: 14-May-2024 at 6:33am


Topic: 'Non-native' DataEntityProperties not getting default values initialized
Posted By: stephenmcd1
Subject: 'Non-native' DataEntityProperties not getting default values initialized
Date Posted: 22-Mar-2012 at 4:43pm
As part of an upgrade from 6.1.3.1 to 6.1.6.0, we've started getting some NullReferenceExceptions when doing queries for certain entities.  I tracked it down to entities which we've added extra DataEntityProperties by doing something like this:
        public partial class PropertyMetadata
        {
            public static readonly DataEntityProperty<MyEntity, bool> ExtraProperty =
                new DataEntityProperty<MyEntity, bool>(EntityPropertyNames.ExtraProperty, false,
                                                       false, ConcurrencyStrategy.None, false, null, false);
        }
During deserialization, the 'get' of this property (which is marked with [DataContract]) throws the NRE in DataEntityProperty<,>.GetValue(TInstance, EntityVersion).  The DevForce code is like this:
return (TValue) obj2.GetValueRaw(this, EntityVersion.Current);
The GetValueRaw returns NULL but then it tries to cast to TValue which is bool in my case.  In debugging this, I see that the way default values get set seems to have changed so I'm thinking this is probably related to that change.

If I create an instance of my entity in memory, the property works fine....for example:
var myEntity = new MyEntity();
var x = myEntity.ExtraProperty; //Does not throw an exception
But I think the default values don't get set up when an entity is being materialized as part of a query from the database?  I'm not entirely sure but I do know that my code is breaking now.  I also tried creating the DataEntityProperty by passing 'true' as the last parameter (isNativeProperty) but that didn't seem to help - I never quite understood what that parameter was for but I thought I might as well try it.

Is this a bug?  Or is there some other way I should be declaring extra, non-DB backed DataEntityProperties?

Thanks,
-Stephen



Replies:
Posted By: sbelini
Date Posted: 27-Mar-2012 at 10:00am
Hi Stephen,
 
Could you provide a solution reproducing the issue. (Against NorthwindIB if possible, please)
 
Regards,
   Silvio.


Posted By: stephenmcd1
Date Posted: 27-Mar-2012 at 11:06am
I was able to reproduce the problem easily with the " http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-silverlight-console - Simple console app (Silverlight) " code sample.  All I did was make a partial class for Customer and try to add a new DataEntityProperty that isn't database backed.  The code looks like this:
using System.Runtime.Serialization;
using IdeaBlade.EntityModel;

namespace SilverlightConsole
{
    public partial class Customer
    {
        [DataMember]
        public bool MyExtraProp
        {
            get { return PropertyMetadata.MyExtraProp.GetValue(this); }
            set { PropertyMetadata.MyExtraProp.SetValue(this, value); }
        }

        public new partial class EntityPropertyNames
        {
            public const string MyExtraProp = "MyExtraProp";
        }

        public partial class PropertyMetadata
        {
            public static readonly DataEntityProperty<Customer, bool> MyExtraProp =
                new DataEntityProperty<Customer, bool>(propertyName: EntityPropertyNames.MyExtraProp,
                                                       isNullable: false,
                                                       isPartOfKey: false,
                                                       concurrencyStrategy: ConcurrencyStrategy.None,
                                                       isAutoIncrementing: false,
                                                       relatedNavigationPropertyName: null,
                                                       isNativeProperty: false);
        }
    }
}

With that change, any requests to load Customers will fail.  The 'getter' of MyExtraProp (that gets called during serialization I believe) will throw the NullReferenceException I originally reported.

I've also attached the entire solution.  All you need to do is run it and the server will throw the exception.  Let me know if you need anything else to reproduce it.   
uploads/613/DataEntityPropertyBug.zip - uploads/613/DataEntityPropertyBug.zip



Posted By: sbelini
Date Posted: 28-Mar-2012 at 3:00pm
Thanks Stephen,
 
We are investigating the issue.
 
Silvio.


Posted By: sbelini
Date Posted: 30-Mar-2012 at 10:31am
Hi Stephen,
 
This was indeed a bug and has already been fixed.
 
The fix will be available in DevForce 6.1.7.
 
Regards,
   Silvio.


Posted By: hijaggu
Date Posted: 22-Jan-2014 at 4:23pm
I have same issue with DataEntityProperty. I am using 6.1.16


Posted By: DenisK
Date Posted: 06-Feb-2014 at 11:52am
Hi hijaggu,

Our tests for the above bug report still pass for 6.1.16. You may have a slightly different scenario. Could you attach a reproducible solution so we may be able to investigate further?



Print Page | Close Window