I'm experiencing issues with Fluent Nhibernate in mapping these relations:
Request entity:
School entity:
Location entity:
Basically, these relations represent a situation where I ask about a school to some web api service. I want to save the request (I put here only the date, but I have a LOT more fields) AND the information retrieved for the school. I also want to bind this information with the request which generated the information. I also want to save the school location and bind this information as before.
What I've done so far:
School.cs:
public class School
{
public virtual string name {get; set;}
public virtual Request request {get; set;}
public virtual string property;
}
Request.cs:
public class Request
{
public virtual int id {get; set;}
public virtual DateTime date {get; set;}
}
Location.cs:
public class Location
{
public virtual bool exist {get; set;}
public virtual School school {get; set;}
public virtual string province {get; set;}
}
SchoolMap.cs
public class SchoolMap : ClassMap<School>
{
public SchoolMap() {
CompositeId().KeyProperty(x => x.name, "name")
.KeyProperty(x => x.request, "request_id");
Map(x => x.property);
}
}
RequestMap.cs
public class Request : ClassMap<Request>
{
public SchoolMap() {
Id(x => x.id).GeneratedBy.Identity();
Map(x => x.date);
}
}
LocationMap.cs
public class LocationMap : ClassMap<Location>
{
public LocationMap() {
CompositeId().KeyProperty(x => x.exist)
.KeyReference(x => x.school, new string[] {"name", "request_id"});
References<School>(x => x.school).Columns(new string [] {"name", "request_id"}).Cascade.All();
Map(x => x.province);
}
}
I also have implemented each repository inheriting from AbstractRepository. The "save" method is (example for SchoolRepository)
public School saveSchool(School school)
{
var transaction = openTransaction();
session.saveOrUpdate(school);
transaction.commit();
return school;
}
This scheme doesn't work. If I create and save (in this order) one request, then one school referencing that request, then one location referencing the school, I receive an "SqlParameter OutOfBound" exception.
I can't figure out how to map this relation.
Thanks in advance to all.
public class EntityC
{
private EntityA _EntityA;
private EntityB _EntityB;
public EntityC()
{
Id = new EntityCId();
}
public virtual EntityCId Id { get; protected set; }
public virtual int EntityA_Id { get { return Id.EntityA_Id; } protected set { Id.EntityA_Id = value; } }
public virtual EntityA EntityA { get { return _EntityA; } set { _EntityA = value; EntityA_Id = value == null ? 0 : value.Id.Id; } }
public virtual int EntityB_Id { get { return Id.EntityB_Id; } protected set { Id.EntityB_Id = value; } }
public virtual EntityB EntityB { get { return _EntityB; } set { _EntityB = value; EntityB_Id = value == null ? 0 : value.Id.Id; } }
}
public class EntityCMap : FluentNHibernate.Mapping.ClassMap<EntityC>
{
public EntityCMap()
{
CompositeId(t => t.Id).KeyProperty(t => t.Id).CustomType<int>()
.KeyProperty(t => t.EntityA_Id).CustomType<int>()
.KeyProperty(t => t.EntityB_Id).CustomType<int>();
Map(t => t.EntityA_Id).Not.Insert().Not.Update();
References(t => t.EntityA).Columns("EntityA_Id");
Map(t => t.EntityB_Id).Not.Insert().Not.Update();
References(t => t.EntityB).Columns("EntityA_Id", "EntityB_Id");
}
}
public class EntityCId
{
public virtual int EntityA_Id { get; set; }
public virtual int EntityB_Id { get; set; }
public virtual int Id { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as EntityCId;
if (t == null)
return false;
if (EntityA_Id == t.EntityA_Id && EntityB_Id == t.EntityB_Id && Id == t.Id)
return true;
return false;
}
public override int GetHashCode()
{
return (EntityA_Id + "|" + EntityB_Id + "|" + Id).GetHashCode();
}
}