Trying to migrate older version to newer one
but getting an error 'unexpected row count 0 expected 1 nhibernate'
Parent Class
using System.Collections; using Iesi.Collections; using Nullables; using Utility; using System;
namespace Business { /// <summary> /// Summary description for Contact. /// </summary> public class Contact { private int id; private string lastName; private string firstName; private string middleName; private string fullName; private string initials; private bool isActive; private string title; private string greeting; private string company; private string notes; private Nullable<Int32> lastChangedUserId; private Nullable<DateTime> lastChangedDate; private string lastChangedReason;
private IList addresses; public virtual int Id { get { return id; } set { id = value; } }
public virtual string LastName { get { return lastName; } set { lastName = value; } }
public virtual string FirstName { get { return firstName; } set { firstName = value; } }
public virtual string MiddleName { get { return middleName; } set { middleName = value; } }
public virtual string FullName { get { return fullName; } set { fullName = value; } }
public virtual string DisplayName { get { string sReturnString = string.Empty; if (lastName != null) // don't want to assign null to this value, it could break things { sReturnString = lastName; }
if ((firstName != null) && (firstName.Length != 0) && (sReturnString.Length > 0)) { sReturnString += ", " + firstName; } else if ((firstName != null) && (firstName.Length != 0)) { sReturnString = firstName; } return sReturnString; } }
public virtual string Initials { get { return initials; } set { initials = value; } }
public virtual bool IsActive { get { return isActive; } set { isActive = value; } }
public virtual string Title { get { return title; } set { title = value; } }
public virtual string Greeting { get { return greeting; } set { greeting = value; } }
public virtual string Company { get { return company; } set { company = value; } }
public virtual string Notes { get { return notes; } set { notes = value; } }
public virtual Nullable<Int32> LastChangedUserId { get { return lastChangedUserId; } set { lastChangedUserId = value; } }
public virtual Nullable<DateTime> LastChangedDate { get { return lastChangedDate; } set { lastChangedDate = value; } }
public virtual string LastChangedReason { get { return lastChangedReason; } set { lastChangedReason = value; } }
public virtual IList Addresses { get { if(addresses == null) addresses = new ArrayList();
return addresses; } set { addresses = value; } }
public virtual ContactAddress PrimaryAddress { get { return ContactAddress.GetBestPrimaryAddressForContact(this.id); } }
public virtual void AddAddress(ContactAddress address) { address.Contact = this; Addresses.Add(address); }
public virtual void Save(int changedByUserId) { lastChangedReason = ""; this.lastChangedUserId = changedByUserId; this.lastChangedDate = DateTime.Today; Save(this); }
public virtual void Delete() { Delete(this); }
public static void Save(Contact contact) { DbSessionFactory.DbSession(DbType.So).SaveOrUpdate(contact); }
public static void Delete(Contact contact) { DbSessionFactory.DbSession(DbType.So).Delete(contact); }
public static Contact Get(int contactId) { return (Contact) DbSessionFactory.DbSession(DbType.So).GetObjectById(typeof (Contact), contactId); }
public static IList List() { return DbSessionFactory.DbSession(DbType.So).GetObjectList(typeof (Contact)); }
public static IList DisplayList() { string sql = "from Contact c order by c.LastName, c.FirstName"; return DbSessionFactory.DbSession(DbType.So).CurrentSession.CreateQuery(sql).List(); } } }
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Business.Contact, Business" table="Contact"> <id name="Id" type="Int32" unsaved-value="0"> <column name="ContactID" unique="true" index="PK_FieldMap"/> <generator class="native" /> </id>
<property name="LastName" column="LastName"/> <property name="FirstName" column="FirstName"/> <property name="MiddleName" column="MiddleName"/> <property name="FullName" column="FullName"/> <property name="Initials" column="Initials"/> <property name="IsActive" column="ContactActive" not-null="true" /> <property name="Title" column="Title"/> <property name="Greeting" column="Greeting"/> <property name="Company" column="Company"/> <property name="Notes" column="Notes"/> <property name="LastChangedUserId" column="LastChangedUser" type="System.Nullable`1[[System.Int32, mscorlib]], mscorlib"/> <property name="LastChangedDate" column="LastChangedDate" type="System.Nullable`1[[System.DateTime, mscorlib]], mscorlib"/> <property name="LastChangedReason" column="LastChangedReason"/> <bag name="Addresses" cascade="delete-orphan" lazy="true" inverse="true"> <key column="ContactID" /> <one-to-many class="Business.ContactAddress, Business" /> </bag>
</class> </hibernate-mapping>
Child class
using System; using System.Collections; using Utility; using Nullables; using NHibernate; using NHibernate.Classic;
namespace Business { /// <summary> /// Summary description for ContactAddress. /// </summary> public class ContactAddress : NHibernate.Classic.ILifecycle { #region ILifeCycle Implementation public virtual LifecycleVeto OnSave(ISession session) { return LifecycleVeto.NoVeto; }
public virtual LifecycleVeto OnUpdate(ISession session) { return LifecycleVeto.NoVeto; }
public virtual LifecycleVeto OnDelete(ISession session) { return LifecycleVeto.NoVeto; }
public virtual void OnLoad(ISession session, object oObject) { this.m_isChanged = false; } ; #endregion
private bool m_isChanged;
private int id; private string name; private string address1; private string address2; private string city; private string state; private string zip; private string country; private bool isPrimary; private Contact contact; private string m_lastchangeduser; private Nullable<DateTime> m_lastchangeddate; private string m_lastchangedreason;
private IList divisions;
public virtual int Id { get { return id; } set { id = value; } }
public virtual string Name { get { return name; } set { m_isChanged |= (name != value); name = value; } }
public virtual string Address1 { get { return address1; } set { m_isChanged |= (address1 != value); address1 = value; } }
public virtual string Address2 { get { return address2; } set { m_isChanged |= (address2 != value); address2 = value; } }
public virtual string City { get { return city; } set { m_isChanged |= (city != value); city = value; } }
public virtual string State { get { return state; } set { m_isChanged |= (state != value); state = value; } }
public virtual string Zip { get { return zip; } set { m_isChanged |= (zip != value); zip = value; } }
public virtual string Country { get { return country; } set { m_isChanged |= (country != value); country = value; } }
public virtual bool IsPrimary { get { return isPrimary; } set { m_isChanged |= (isPrimary != value); isPrimary = value; } }
/// <summary> /// /// </summary> public virtual string LastChangedUser { get { return m_lastchangeduser; }
set { m_lastchangeduser = value; } } /// <summary> /// /// </summary> public virtual Nullable<DateTime> LastChangedDate { get { return m_lastchangeddate; } set { m_lastchangeddate = value; }
} /// <summary> /// /// </summary> public virtual string LastChangedReason { get { return m_lastchangedreason; }
set { m_isChanged |= (m_lastchangedreason != value); m_lastchangedreason = value; } }
public virtual Contact Contact { get { return contact; } set { contact = value; } }
/// <summary> /// Returns whether or not the object has changed it's values. /// </summary> public virtual bool IsChanged { get { return m_isChanged; }
set { m_isChanged = value; } }
public virtual IList Divisions { get { if(divisions == null) divisions = new ArrayList(); return divisions; } set { divisions = value; } }
public virtual void Save() { // as always and just update the DateTime stuff if needed. if (m_isChanged) { LastChangedDate = DateTime.Today; LastChangedUser = Utility.UserSettings.Current.Username; }
Save(this); m_isChanged = false; }
public virtual void Delete() { Delete(this); }
public override bool Equals(object obj) { //Check for null and compare run-time types. if (obj == null || GetType() != obj.GetType()) return false;
return this.id == ((ContactAddress)obj).id; }
public override int GetHashCode() { return this.id.GetHashCode(); }
public static ContactAddress Get(int addressId) { ContactAddress oReturnAddress = (ContactAddress)DbSessionFactory.DbSession(DbType.So).GetObjectById(typeof (ContactAddress), addressId); return oReturnAddress; }
public static ContactAddress GetBestPrimaryAddressForContact(int contactId) { string formatSql = "from ContactAddress ca where ca.Contact.Id = {0} order by IsPrimary desc"; string sql = string.Format(formatSql, contactId);
IList addresses = DbSessionFactory.DbSession(DbType.So).CurrentSession.CreateQuery(sql).List();
if(addresses != null && addresses.Count > 0) return (ContactAddress)addresses[0]; else return null; }
public static void Save(ContactAddress address) { DbSessionFactory.DbSession(DbType.So).SaveOrUpdate(address); }
public static void Delete(ContactAddress address) { DbSessionFactory.DbSession(DbType.So).Delete(address); } } }
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class name="Business.ContactAddress, Business" table="ContactAddress"> <id name="Id" column="ContactAddressID" type="Int32" unsaved-value="0"> <generator class="native" /> </id> <property name="Name" column="ContactAddressName"/> <property name="Address1" column="Address"/> <property name="Address2" column="Address2"/> <property name="City" column="City"/> <property name="State" column="State"/> <property name="Zip" column="ZipCode"/> <property name="Country" column="Country"/> <property name="IsPrimary" column="IsPrimary" /> <property column="LastChangedUser" type="String" name="LastChangedUser" /> <property column="LastChangedDate" type="System.Nullable`1[[System.DateTime, mscorlib]], mscorlib" name="LastChangedDate" /> <property column="LastChangedReason" type="String" name="LastChangedReason" /> <bag name="Divisions" lazy="true" cascade="none" table="DivisionAddress"> <key column="ContactAddressId"></key> <many-to-many class="Business.Division, Business" column="DivisionId" /> </bag> <many-to-one name="Contact" column="ContactID" class="Business.Contact, Business" /> </class> </hibernate-mapping>
on save in UI saving like this
private void savecontact(Contact contact) {
DbSessionFactory.DbSession.FlushSession(); DbSessionFactory.DbSession.CloseSession(); DbSessionFactory.DbSession.OpenSession();
Contact contact = Contact.Get(contactId);
ArrayList newAddresses = new ArrayList(); ArrayList newAddressIds = new ArrayList();
for (int i = 0; i < addressDataSource.Rows.Count; i++ ) { datarow row = addressDataSource.Rows[i]; int addressId = (int)row.GetCellValue("AddressId");
ContactAddress address; if (addressId > 0) { address = ContactAddress.Get(addressId); if (address != null) { newAddressIds.Add(addressId); } else { address = new ContactAddress(); //newAddressIds.Add(addressId); } } else { address = new ContactAddress(); }
if (address != null) { // address details entererd
newAddresses.Add(address); } }
foreach(ContactAddress address in contact.Addresses) { bool found = false; foreach(int addressId in newAddressIds) { if(addressId == address.Id || addressId == 0) { found = true; break; } }
if(!found) { if (ContactAddressRemoved != null) ContactAddressRemoved(this, new AddressDeletedEventArgs(address));
address.Delete(); //address.Divisions.Clear(); } }
contact.Addresses.Clear();
foreach (ContactAddress address in newAddresses) { address.Save(); contact.Addresses.Add(address); }
contact.Save(UserUtil.CurrentUser); }
thank you, Vidya
|