客戶可以有多個聯繫人。下面的代碼正在工作,但...此客戶的聯繫人將首先被刪除,然後再次插入。是不可能避免這種刪除,只需插入新的?NHibernate:多對多集合
<class name="Customer" table="Customer">
<id name="Id">
<generator class="native" />
</id>
<property name="LastName" not-null="true" />
<bag name="Contacts" cascade="all-delete-orphan" table="CustomerContact">
<key column="Customer" not-null="true" />
<many-to-many class="Contact"/>
</bag>l
</class>
<class name="Contact" table="Contact">
<id name="Id">
<generator class="native" />
</id>
<property name="LastName" not-null="true" />
<many-to-one name="Customer" column="Customer" not-null="true" />
</class>
public virtual void AddContact(Contact contact)
{
Contacts.Add(contact);
contact.Customer = this;
}
當我做這個代碼兩次,加2個聯繫人:
Contact contact = new Contact() { LastName = "MyLastName" };
Customer customer = session.Get(customerId);
customer.AddContact(contact);
session.Update(customer);
更改後不需要致電更新(客戶)。它在會話中會自動更新。 –
聯繫人如何實施? –