Skip to content

Access Proxy Id through interface triggers Lazy Load #2184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MagicShoebox opened this issue Jun 25, 2019 · 3 comments
Closed

Access Proxy Id through interface triggers Lazy Load #2184

MagicShoebox opened this issue Jun 25, 2019 · 3 comments

Comments

@MagicShoebox
Copy link

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!

@fredericDelaporte
Copy link
Member

Your case will not be fixed. You should remove the explicit implementation and only use the implicit ones. Then it should work with NHibernate 5.3, but it may depend on how we fix #2085, if we fix it.

With an explicit implementation of an interface, if the entity is mapped as the concrete class, there are no way NHibernate could tell if the Id property from the interface will actually match the Id from the mapped concrete class. So instead of doing some wild guess, it must trigger the proxy loading to simply call it and see what it yields.

@MagicShoebox
Copy link
Author

MagicShoebox commented Aug 4, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants