Display foreign key in Fluent NHibernate without object property

My question is: is there a possible Fluent NHibernate mapping for parent and child objects that do not require the Child object to have the property of the parent object? I did not understand how to map the link to the parent. When I call Create with as-is mappings, I get an exception because the Child object does not have the required foreign key (required in the data store) back to the parent.

I have two POCO classes:

public class Parent { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual IList<Child> Childs { get; set; } } public class Child { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual int ParentId { get; set; } } 

And some comparisons:

 public class ParentMap : ClassMap<Parent> { public ParentMap() { this.Table("Parents"); this.Id(x => x.Id); this.Map(x => x.Name); this.HasMany(x => x.Childs).KeyColumn("ChildId").Cascade.AllDeleteOrphan(); } } public class ChildMap : ClassMap<Child> { public ChildMap() { this.Table("Childs"); this.Id(x => x.Id); this.Map(x => x.Name); // Needs some sort of mapping back to the Parent for "Child.ParentId" } } 

And create a method:

 public Parent Create(Parent t) { using (this.session.BeginTransaction()) { this.session.Save(t); this.session.Transaction.Commit(); } return t; } 

I want to create a parent object that has a list of Child objects, but it does not have Child objects that return them to their parents (except for the parent identifier). I want to do this in order to avoid a circular reference from the parent to the list of children back to the parent object, as this causes problems with JSON serialization.

+6
source share
1 answer

You can map these objects without problems, try the following:

 public class ParentMap : ClassMap<Parent> { public ParentMap() { this.Table("Parents"); this.Id(x => x.Id); this.Map(x => x.Name); this.HasMany(x => x.Childs).KeyColumn("ChildId").Cascade.AllDeleteOrphan(); } } public class ChildMap : ClassMap<Child> { public ChildMap() { this.Table("Childs"); this.Id(x => x.Id); this.Map(x => x.Name); this.Map(x => x.ParentId); // if you have a reference of Parent object, you could map as a reference, for sample: this.References(x => x.Parent).Column("ParentId"); } } 

When you receive objects from ISession, do not serialize it in some format, because it can be nhibernate proxy objects instead of object objects. Try to create DTO classes (Data Transfer Object) and convert these objects to a DTO object and serialize it. You will avoid circular links. For sample:

 public class ParentDTO { public int Id { get; set; } public string Name { get; set; } public int ParentId { get; set; } /* here you just have the primitive types or other DTOs, do not reference any Entity type*/ } 

And when you need to read the values ​​to share the serialized value:

 var dto = ISession.Query<Parent>() .Select(x => new ParentDTO() { Id = x.Id, Name = x.Name, ParentId = x.ParentId) .ToList(); 

Get this result from the data access level and try serializing the sample:

 var result = Serialize(dto); 
+3
source

Source: https://habr.com/ru/post/949351/


All Articles