New Posts New Posts RSS Feed: Minor bug with Entity Key
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Minor bug with Entity Key

 Post Reply Post Reply
Author
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Topic: Minor bug with Entity Key
    Posted: 09-Mar-2012 at 12:28pm
Hi stephenmcd1,

Thank you for the feedback. I'll file a bug report for this.
Back to Top
stephenmcd1 View Drop Down
DevForce MVP
DevForce MVP


Joined: 27-Oct-2009
Location: Los Angeles, CA
Posts: 166
Post Options Post Options   Quote stephenmcd1 Quote  Post ReplyReply Direct Link To This Post Posted: 08-Mar-2012 at 3:21pm
If you pass in an array of value types to the constructor of Entity Key, you get an exception.  For example:
var myKeyValues = new [] {123, 456}; //Compiler decides this should be an int[]
var key = new EntityKey(typeof(MyEntity), myKeyValues);
The EntityKey constructor will throw an exception like:
System.InvalidCastException: Unable to cast object of type 'System.Int32[]' to type 'System.Object[]'.
   at IdeaBlade.EntityModel.EntityKey..ctor(Type entityType, Object aValue, Boolean convertValue)
Looking at it in Reflector, it seems that constructor assumes that if the passed in value is an Array, it can safely cast it to an object array (object[]).  But that is only true if the source array is of reference types (which is not the case in my example because I'm passing in an array of ints).

The workaround for this is pretty easy but I think it's still worth fixing (we just ran into this in a part of our code that doesn't get hit very often so we 'forgot' to use the workaround).  For reference, the following code will work without a problem.
var myKeyValues = new object[] {123, 456}; // Force boxing of the ints to workaround Dev Force issue
var key = new EntityKey(typeof(MyEntity), myKeyValues);
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down