diff --git a/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs b/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs
new file mode 100644
index 00000000..ecc70e78
--- /dev/null
+++ b/Src/NHibernate.Envers.Tests/Async/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs
@@ -0,0 +1,41 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated by AsyncGenerator.
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+
+using System.Linq;
+using NUnit.Framework;
+using SharpTestsEx;
+
+namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional
+{
+ using System.Threading.Tasks;
+ public partial class LazyPropertyBidirectionalTest : TestBase
+ {
+
+ [Test]
+ public async Task SavePersonProxyForFieldInterceptorAsync()
+ {
+ long carId;
+ using (var tx = Session.BeginTransaction())
+ {
+ var pers = Session.Query().Single(x => x.Id == id_pers1);
+ var car = new Car
+ {
+ Owner = pers
+ };
+ pers.Cars.Add(car);
+ carId = (long) await (Session.SaveAsync(car)).ConfigureAwait(false);
+ await (tx.CommitAsync()).ConfigureAwait(false);
+ }
+
+ (await (AuditReader().FindAsync(carId, 2)).ConfigureAwait(false)).Owner.Name
+ .Should().Be.EqualTo("Hernan");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs
new file mode 100644
index 00000000..b06882be
--- /dev/null
+++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Car.cs
@@ -0,0 +1,12 @@
+using NHibernate.Envers.Configuration.Attributes;
+
+namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional
+{
+ [Audited]
+ public class Car
+ {
+ public virtual long Id { get; set; }
+ public virtual int Number { get; set; }
+ public virtual Person Owner { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs
new file mode 100644
index 00000000..f59262e2
--- /dev/null
+++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/LazyPropertyTest.cs
@@ -0,0 +1,46 @@
+using System.Linq;
+using NUnit.Framework;
+using SharpTestsEx;
+
+namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional
+{
+ public partial class LazyPropertyBidirectionalTest : TestBase
+ {
+ private long id_pers1;
+
+ public LazyPropertyBidirectionalTest(AuditStrategyForTest strategyType) : base(strategyType)
+ {
+ }
+
+ protected override void Initialize()
+ {
+ var pers1 = new Person {Name = "Hernan"};
+
+ using (var tx = Session.BeginTransaction())
+ {
+ id_pers1 = (long) Session.Save(pers1);
+ tx.Commit();
+ }
+ }
+
+ [Test]
+ public void SavePersonProxyForFieldInterceptor()
+ {
+ long carId;
+ using (var tx = Session.BeginTransaction())
+ {
+ var pers = Session.Query().Single(x => x.Id == id_pers1);
+ var car = new Car
+ {
+ Owner = pers
+ };
+ pers.Cars.Add(car);
+ carId = (long) Session.Save(car);
+ tx.Commit();
+ }
+
+ AuditReader().Find(carId, 2).Owner.Name
+ .Should().Be.EqualTo("Hernan");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml
new file mode 100644
index 00000000..9bd70ede
--- /dev/null
+++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Mapping.hbm.xml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs
new file mode 100644
index 00000000..82287184
--- /dev/null
+++ b/Src/NHibernate.Envers.Tests/NetSpecific/Integration/ManyToOne/LazyProperty/Bidirectional/Person.cs
@@ -0,0 +1,13 @@
+using NHibernate.Envers.Configuration.Attributes;
+using System.Collections.Generic;
+
+namespace NHibernate.Envers.Tests.NetSpecific.Integration.ManyToOne.LazyProperty.Bidirectional
+{
+ [Audited]
+ public class Person
+ {
+ public virtual long Id { get; set; }
+ public virtual string Name { get; set; }
+ public virtual ISet Cars { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/Src/NHibernate.Envers/Event/AuditEventListener.cs b/Src/NHibernate.Envers/Event/AuditEventListener.cs
index c9306b4e..ef31c37c 100644
--- a/Src/NHibernate.Envers/Event/AuditEventListener.cs
+++ b/Src/NHibernate.Envers/Event/AuditEventListener.cs
@@ -81,20 +81,17 @@ private void addCollectionChangeWorkUnit(AuditProcess auditProcess, ISessionImpl
{
// relDesc.getToEntityName() doesn't always return the entity name of the value - in case
// of subclasses, this will be root class, no the actual class. So it can't be used here.
- string toEntityName;
object id;
+ var toEntityName = session.BestGuessEntityName(value);
if (value is INHibernateProxy newValueAsProxy)
{
- toEntityName = session.BestGuessEntityName(value);
id = newValueAsProxy.HibernateLazyInitializer.Identifier;
// We've got to initialize the object from the proxy to later read its state.
value = Toolz.GetTargetFromProxy(session, newValueAsProxy);
}
else
{
- toEntityName = session.GuessEntityName(value);
-
var idMapper = VerCfg.EntCfg[toEntityName].IdMapper;
id = idMapper.MapToIdFromEntity(value);
}