在ActiveRecord中插入父级-子级
我正在使用CastleActiveRecord映射数据库 我有这样一个简单的数据库: 和映射代码: 组:在ActiveRecord中插入父级-子级,activerecord,key,composite,castle,Activerecord,Key,Composite,Castle,我正在使用CastleActiveRecord映射数据库 我有这样一个简单的数据库: 和映射代码: 组: [ActiveRecord("[Group]")] public class Group : ActiveRecordBase { private long m_ID; private string m_GroupName; private string m_Description; private IList&
[ActiveRecord("[Group]")]
public class Group : ActiveRecordBase
{
private long m_ID;
private string m_GroupName;
private string m_Description;
private IList<Contact> m_Contacts = new List<Contact>();
[PrimaryKey(Column = "`ID`", Generator = PrimaryKeyType.Identity)]
public long ID
{
get { return m_ID; }
set { m_ID = value; }
}
[Property(Column = "`GroupName`", NotNull = true)]
public string GroupName
{
get { return m_GroupName; }
set { m_GroupName = value; }
}
[Property(Column = "`Description`", NotNull = false)]
public string Description
{
get { return m_Description; }
set { m_Description = value; }
}
[HasAndBelongsToMany(typeof(Contact),
Table = "`Contact_Group`",
ColumnRef = "`ContactID`",
ColumnKey = "`GroupID`")]
public IList<Contact> Contacts
{
get { return m_Contacts; }
set { this.m_Contacts = value; }
}
public static Group[] FindAll()
{
try
{
return (Group[])ActiveRecordBase.FindAll(typeof(Group));
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
现在我想在联系人表中插入一个新联系人,并在联系人组表中添加一组联系人组。这是我的代码:
public void Save()
{
try
{
if (this.view.CheckFields())
{
// Add new contact
if (this.contact == null)
{
Contact c = new Contact();
c.FirstName = this.view.GetFirstName();
c.LastName = this.view.GetLastName();
c.Number = this.view.GetNumber();
c.Sex = this.view.GetSex();
c.DayOfBirth = this.view.GetDayOfBirth();
c.Nationality = this.view.GetNationality();
c.ExtraNumber = this.view.GetExtraNumber();
c.Email = this.view.GetEmail();
c.Address = this.view.GetAddress();
c.Company = this.view.GetCompany();
c.Career = this.view.GetCareer();
c.ExtraInfo = this.view.GetExtraInfo();
// Check if the contact's number is valid
if (IsValidNumber(c.Number))
{
Group[] gs = this.view.GetGroupList();
foreach (Group g in groups)
{
c.Groups.Add(g);
}
c.CreateAndFlush();
}
}
else // Modify a contact
{
}
}
}
catch (OleDbException e)
{
MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
但在执行Save()之后,它只插入一个新联系人,而不插入联系人组。
映射或Save()方法中是否有错误?
请帮帮我。提前谢谢。
P/S:我是NHibernate的新手。
我尝试了这段代码,它成功了==
using (new SessionScope())
{
Group[] gs = this.view.GetGroupList();
foreach (Group g in groups)
{
contact.Groups.Add(g);
}
contact.Save();
}
public void Save()
{
try
{
if (this.view.CheckFields())
{
// Add new contact
if (this.contact == null)
{
Contact c = new Contact();
c.FirstName = this.view.GetFirstName();
c.LastName = this.view.GetLastName();
c.Number = this.view.GetNumber();
c.Sex = this.view.GetSex();
c.DayOfBirth = this.view.GetDayOfBirth();
c.Nationality = this.view.GetNationality();
c.ExtraNumber = this.view.GetExtraNumber();
c.Email = this.view.GetEmail();
c.Address = this.view.GetAddress();
c.Company = this.view.GetCompany();
c.Career = this.view.GetCareer();
c.ExtraInfo = this.view.GetExtraInfo();
// Check if the contact's number is valid
if (IsValidNumber(c.Number))
{
Group[] gs = this.view.GetGroupList();
foreach (Group g in groups)
{
c.Groups.Add(g);
}
c.CreateAndFlush();
}
}
else // Modify a contact
{
}
}
}
catch (OleDbException e)
{
MessageBox.Show(e.Message, "Loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
using (new SessionScope())
{
Group[] gs = this.view.GetGroupList();
foreach (Group g in groups)
{
contact.Groups.Add(g);
}
contact.Save();
}