KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to get NHibernate to use the many side of a collection to manage a bidirectional association to model a zero-to-one relationship. Parent Class and Map: public class Parent { private ICollection<Child> children; public Parent() { this.children = new HashedSet<Child>(); } public virtual Guid Id { get; protected internal set; } public virtual Child Child { get { return children.FirstOrDefault(); } set { { this.children.Clear(); if (value != null) { this.children.Add(value); } } } } } public class ParentMap : ClassMap<Parent> { public ParentMap() { this.Id(x => x.Id) .GeneratedBy.GuidComb(); this.HasMany<Child>(Reveal.Member<Parent>("children")) .Access.Field() .Cascade.All() .Not.Inverse() .AsSet(); } } Child Class and Map: public class Child { public virtual Guid Id { get; protected internal set; } public virtual Parent Parent { get; set; } } public class ChildMap : ClassMap<Child> { public ChildMap() { this.Id(x => x.Id) .GeneratedBy.GuidComb(); this.References(x => x.Parent) .Not.Nullable() .Cascade.All(); } } The following code produces two inserts and an update: var parent = new Parent(); var child = new Child(); parent.Child = child; child.Parent = parent; session.Save(parent); session.Flush(); Notice the essentially duplicate SQL for the second insert and the following update: exec sp_executesql N'INSERT INTO [Parent] (Id) VALUES (@p0)',N'@p0 uniqueidentifier',@p0='AA5A146E-E3F5-4373-B7A8-9EF301171401' go exec s
Tags (comma-separated)
Save Edits
Cancel