NHibernate Prevent Lazy Loading of unmatched reference

前端 未结 2 835
北海茫月
北海茫月 2021-01-25 13:10

I have quite a problem with NHibernate. I have a reference from Table1 to Table2, and I want NHibernate to, when a corresponding record is not found in Table2, to not then issue

相关标签:
2条回答
  • 2021-01-25 13:47

    try

    .Not.LazyLoad(); 
    

    instead of .LazyLoad(laziness.false);

    0 讨论(0)
  • 2021-01-25 13:50

    It is correct, that NHibernate tries to load "not existing". It must do that.

    As stated here Ayende - NHibernate Mapping (an extract):

    12) not-found is another legacy feature, it controls how NHibernate behaves when it finds an invalid foreign key. That is, a value that points to an entity that doesn’t exist. By default, this would trigger an error, as this generally indicate a problem with the database, but with legacy database, you can tell it to set the property value to null instead.

    And as could be found here: Lazy loading for NHibernate with Ignore.NotFound (an extract):

    When you specify the .NotFound().Ignore() this forces the entity to be eagerly loaded and cannot be overriden with the .LazyLoad(). NHibernate does this because it has to be sure that relationship exists or doesn't exist since you are not relying on the database to enforce this.

    And here Why Nhibernate won't lazy load my many-to-one relationship?, José F. Romaniello says:

    This is your problem, nhibernate must to be sure, that an invoice EXIST or do not exist for each enrollment.

    I'd strongly recommend you to fix your data problems and remove the not-found="ignore" attribute. It is a bad thing.

    0 讨论(0)
提交回复
热议问题