Let's imagine a situation.

[HierarchyRoot]
    public abstract class AbstractObject : Entity
    {
        [Field, Key]
        public Guid Id { get; private set; }
        ...
    }

    public abstract class BlogPost : AbstractObject 
    {
        [Field]
        public BlogCategory Category { get; set; }
        ...
    }

    public sealed class BlogCategory : AbstractObject
    {
        ...
    }

I want to get an object with some ID.

var post = Query<BlogPost>.SingleOrDefault(id);

If this ID is Category's ID, I get an exception instead of returning NULL. I think it is not a right behavior.


Updated at 28.10.2009 14:58:16

[InvalidCastException: Unable to cast object of type 'W3P.Model.BlogCategory' to type 'W3P.Model.BlogPost'.] Xtensive.Storage.Query`1.SingleOrDefault(Object[] keyValues) in c:\TeamCity\buildAgent\work\6a5d81309423096e\Xtensive.Storage\Xtensive.Storage\Query{T}.cs:87

...


Updated at 28.10.2009 15:09:12

Yes. I suppose a category object was fetched earlier and stored in cache.

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by buck.

asked Oct 28 '09 at 14:32

Editor's gravatar image

Editor
46154156157

Thanks!

It seems that we successfully fetched the instance of BlogCategory type by given ID and tried to cast it to the required type (BlogPost). At least, there could be thrown more adequate exception, but NULL as a result is preferable.

To be fixed.

(Oct 28 '09 at 14:32) Dmitri Maximov Dmitri%20Maximov's gravatar image

Yep, you were definitely right. Entity of wrong type (but from the same hierarchy) was taken from Session cache and Key.Type check was omitted.

Fixed. See nightly build.

(Oct 28 '09 at 14:32) Dmitri Maximov Dmitri%20Maximov's gravatar image

Could you provide us with the stack trace? This will be quite useful.

(Oct 28 '09 at 14:32) Dmitri Maximov Dmitri%20Maximov's gravatar image
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA