fluent-nhibernate
fluent-nhibernate copied to clipboard
Data not found error when mapping property (object) to Function(return type table)
Hi all,
I have a problem, when I'm trying map function from DB to property (obj).
- Design from database [Contact] has many [ContactDetail]

And only one ContactDetail was in current which will be loaded in first time connect to DB with Contact, but in progress the data result throw an exception " Data not found" although the data in DB always be available.
this is my code below:
My interfaces defination
public interface IContact
{
int id {get;set;}
string NINumber { get; set; }
IList<IContactDetailActive > ContactDetailList {get;set}
// Only one contact detail in current
IContactDetailActive ContactDetailActive {get;set;}
}
public interface IContactDetail
{
int id {get;set;}
string Description{ get; set; }
IContact Contact {get;set;}
}
Implementation classes
public interface Contact : IContact
{
public virtual int id {get;set;}
public virtual string NINumber { get; set; }
public virtual IList<IContactDetailActive > ContactDetailList { get; set; }
public virtual IContactDetailActive ContactDetailActive {get;set;}
}
public interface ContactDetail : IContactDetail
{
public virtual int id {get;set;}
public virtual string Description{ get; set; }
public virtual IContact Contact {get;set;}
}
Class map
/// <summary>
/// Contact mapping class.
/// </summary>
public class ContactMap : ClassMap<Contact>
{
/// <summary>
/// Initializes a new instance of the <see cref="ContactMap"/> class.
/// </summary>
public ContactMap()
{
this.LazyLoad();
this.Id(x => x.Id).GeneratedBy.Native().Column("ContactID");
this.Map(x => x.NINumber).Length(20).Nullable();
this.HasMany<ContactDetail>(x => x.ContactDetailList).KeyColumn("ContactID").Cascade.All().Inverse();
this.Reference<ContactDetail>(x => x.ContactDetailActive).KeyColumn("ContactID").Formular("dbo.GetContactDetailActive(ContactID)").Not.Insert().Not.Update();
}
}
/// <summary>
/// Contact detail mapping Definition.
/// </summary>
public class ContactDetailMap : CxEntityWithEffectiveDateMap<ContactDetail>
{
/// <summary>
/// Initializes a new instance of the <see cref="ContactDetailMap"/> class.
/// </summary>
public ContactDetailMap()
{
this.LazyLoad();
this.Id(x => x.Id).Column("ContactDetailId").GeneratedBy.Identity();
this.Map(x => x.Description).Nullable();
References<Contact>(x => x.Contact).Column("ContactId");
}
}
Create FUNCTION [dbo].[GetContactDetailActive](@ContactId int)
RETURNS TABLE
AS RETURN
(
SELECT TOP 1 * FROM ContactDetail WHERE ContactId = @ContactId ORDER BY ContactDetailId DESC
);
Someone else has the same problem ? I need your help