Closed
Description
CBP created an issue — 31st July 2014, 2:28:27:
I have an entity that has a collection of components mapped.
The Fluent NH mapping like this:
public sealed class FooDbMap : ClassMap<Foo> { public FooDbMap() { ... etc HasMany(x => x.Bars) .Component(part => { part.References(x => x.Qux, "QuxId").LazyLoad().Nullable(); part.Map(x => x.Baz).Nullable(); }) .AsSet() .KeyColumn("FooId") .LazyLoad() .Table("sfFooBars"); } }The component class looks like this:
public class Bar { public virtual Qux Qux { get; set; } public virtual string Baz { get; set; } public bool Equals(Bar other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return Equals(other.Qux, Qux) && Equals(other.Baz, Baz); } public override int GetHashCode() { unchecked { int result = (Qux != null ? Qux.GetHashCode() : 0); result = (result*397) ^ (Baz != null ? Baz.GetHashCode() : 0); return result; } } }The following test demonstrates the problem:
[Test] public void Test() { var foo = new Foo(); foo.AddBar(new Bar { Qux = null, Baz = "asdf" }); CurrentSession.Save(foo); CurrentSession.Flush(); CurrentSession.Clear(); var reloadedFoo = CurrentSession.Load<Foo>(); reloadedFoo.RemoveBar(reloadedFoo.Bars.First()); CurrentSession.Flush(); }This test fails to remove the bar.
The reason is because the generated SQL is incorrect:exec sp_executesql N'DELETE FROM Foo WHERE FooId = @p0 AND Baz = @p1 AND Qux = @p2',N'@p0 uniqueidentifier,@p1 varchar(8000),@p2 uniqueidentifier',@p0='494464F4-6B1A-455F-8786-A37900C9CE36',@p1='asdf',@p2=NULLThe correct SQL should check if Qux is null using the syntax "Qux is null", rather than "Qux = @p2".
Alexander Zaytsev added a comment — 12th October 2014, 10:49:27:
A test case would be appreciated