Andrej Kaurin
Andrej Kaurin

Reputation: 11642

NHibernate (Fluent mapping) ReferencesAny<object>

Base entity interface is IEntity which requires only "object ID {get;set;}" to be implemented. Now, in almost every case ID is of Guid type (except for membership etc). I am using following code to do mapping of interface

...
    AnyPart<IEntity> primaryMap = ReferencesAny(x => x.Primary)
                    .IdentityType<object>() // tried with .IdentityType<Guid>()
                    .EntityIdentifierColumn("PrimaryID")
                    .EntityTypeColumn("PrimaryType")
                    .MetaType<string>();
...

Of course, next I am adding meta values.

So, Now getting error

Source array was not long enough. Check srcIndex and length, and the array's lower bound

And with .IdentityType<Guid>()

could not resolve property: Primary.ID of: Founder.Connection [.SingleOrDefault[Founder.Connection](NHibernate.Linq.NhQueryable`1[Founder.Connection], Quote((x, ) => (OrElse(AndAlso(Equal(x.Primary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211), Equal(x.Secondary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211)), AndAlso(Equal(x.Secondary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211), Equal(x.Primary.ID, 35c2142a-4c17-4b77-96fd-a2570028a211))))), )]

UPDATE:

I tried also with .IdentityType(x=>x.ID) but same problem (Source array was not long enough)

UPDATE II:

Query (Actually whole method containing query) that this error occurs on is bellow:

public IQueryable<Connection> GetConnections(IEntity connectable)
        {
            IQueryable<Connection> query =
                Query().Where(
                    x => x.Primary.ID == connectable.ID || x.Secondary.ID == connectable.ID);
            return query;
        }

Upvotes: 1

Views: 384

Answers (1)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

Try this in the query: x.Primary == connectable (without ID).

The problem is that you reference an object (or another unmapped type, that's why you need an any mapping). There is no ID on object.

By the way, using HQL would allow you to access the id by using the keyword id, which is not available in Linq (technically it could be made available as extension method, but I don't know Linq to NH good enough to say if it had been implemented). Every any reference conceptually has an id and a class.

Upvotes: 1

Related Questions