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; //getters and setters
public virtual IList Addresses { get { if(addresses == null) addresses = new ArrayList();
return addresses; } set { addresses = value; } } 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;
//getters and setters 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 static void Save(ContactAddress address) { DbSessionFactory.DbSession.SaveOrUpdate(address); }
public static void Delete(ContactAddress address) { DbSessionFactory.DbSession.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); }
any help is great thank you, Vidya
|