Let me start by displaying the mapping:
Parent:
<bag name="Communicatiekanalen" table="COMMUNICATIEKANAAL" inverse="true" cascade="delete" lazy="true" >
<key column="SEK_PROFIEL"/>
<one-to-many class="Crm.Hibernate.Communicatiekanaal,Crm.Hibernate" />
</bag>
Child:
<many-to-one name="SekProfiel" column="SEK_PROFIEL" class="Crm.Hibernate.Profiel,Crm.Hibernate" />
In other words: a profile can have many communication channels.
The following event is triggered in the user interface (user interface [ASP.NET Webforms]) (deleting a profile with communication channels connected to it):
var profielDao = CrmConfiguration.GetDaoFactory().GetProfielDao();
var profiel = profielDao.GetById(2194, true);
profielDao.Delete(profiel);
(DaoFactory is in the same project file, and the user interface is the ASP.NET website)
This code works.
IMPORTANT: The code uses the NHibernate 'open-session-in-view' template.
I have a service implementation that runs the same code (deleting a profile with communication channels). Some code ...
var daof = CrmConfiguration.GetDaoFactory();
CrmSettings.Instance.UserID = user;
var profielDao = daof.GetProfielDao();
profielDao.BeginTransaction();
var profiel = profielDao.GetById(CrmEntitiesToHibernate.ParseStringToId(profileId), true);
profielDao.Delete(profiel);
profielDao.EndTransaction();
Where 'EndTransaction ()' executes 'commit'. I am testing this code with unit test ':
[TestMethod]
public void TestDeleteProfile()
{
var profile = GetSecundaryProfile();
CrmClient.AddCommunicationChannelForProfile(GetPlainCommunicationChannel(), profile.Id, CurrentUserId);
CrmClient.DeleteProfile(profile.Id, CurrentUserId);
}
This code does not work. The following error calls me:
DELETE REFERENCE "R2_PROFIEL". "CRM_ontw", "dbo.COMMUNICATIEKANAAL", 'SEK_PROFIEL. .
, . UI, , . , ?
: :
public void Delete(T entity)
{
try
{
OnDelete(entity);
}
catch (Exception)
{
SessionManager.Instance.RollbackTransactionOn(SessionFactoryConfigPath);
throw;
}
session.Delete(entity);
}
all-delete-orphan .