Skip to content

Access Proxy Id through interface triggers Lazy Load #2184

Closed
@MagicShoebox

Description

@MagicShoebox

Hi,

I've encountered an issue where accessing the Id property of a Proxy via an interface causes the Entity to be loaded.

public interface IEntity
{
    int Id { get; }
}

public class Entity : IEntity
{
    public virtual int EntityId { get; set; }

    int IEntity.Id => EntityId;
}

public void Test(ISessionFactory sessionFactory, int id)
{
    Entity entity;
    IEntity iEntity;
    using (ISession session = sessionFactory.OpenSession())
    {
        var x = session.Load<Entity>(id);
        entity = x;
        iEntity = x;
    }

    Assert.Equal(id, entity.EntityId);  // OK
    Assert.Equal(id, iEntity.Id);       // LazyInitializationException
}

I saw several threads that looked related, but unfortunately they dealt with more complicated situations (subclasses, etc.) and I'm not well-versed in NHibernate's internals. Most seem to indicate issues had been fixed, though. My issue is showing up on 5.2.5.

Is there something I am not understanding, or a workaround available?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions