Delete on Many to Many Relationship using Fluent Nhibernatev

111 views Asked by At

Please, help me! I can't delete objects and dependence in Oid and CertRequest table.

I have follow:

Oid

public class Oid { public virtual int Id { get; protected set; }
public virtual int RowVersion { get; set; }
public virtual string Code { get; set; } public virtual string Name { get; set; } }

public class OidMap : IAutoMappingOverride<Oid>
{
    public void Override(AutoMapping<Oid> mapping)
    {
        mapping.Map(c => c.Code).Not.Nullable().Unique();
        mapping.Map(c => c.Name).Not.Nullable().Length2000();
    }
}

CertRequest

public class CertRequest
{
    public virtual int Id { get; protected set; }  
    public virtual int RowVersion { get; set; }      
    public virtual string LastName { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string MiddleName { get; set; }
    public virtual ICollection<Oid> Oids { get; set; }
}

public class CertRequestMap : IAutoMappingOverride<CertRequest>
{
    public void Override(AutoMapping<CertRequest> mapping)
    {
        mapping.Map(c => c.LastName).Nullable().Length2000();
        mapping.Map(c => c.FirstName).Nullable().Length2000();
        mapping.Map(c => c.MiddleName).Nullable().Length2000();
        mapping.HasManyToMany(c => c.Oids)
            .Table("CertRequestToOid")
            .ParentKeyColumn("CertRequestId")
            .ChildKeyColumn("OidId");
    }
}

Deleting code:

certRequest.Oids.Clear();

_certRequestRepository.SaveOrUpdate(certRequest);

_certRequestRepository.Delete(certRequest);

Do nothing.

Any tip...

UPDATE

I am sorry, i not said that i use Sharp-Architecture, and Transaction attribute on method.

1

There are 1 answers

1
Andrew Shepherd On BEST ANSWER

Is _certRequestRepository an object of type ISession?

If so, you will have to call _certRequestRepository.Flush() to commit the delete to the database.

Better yet, use a Transaction.

using(var transaction = _certRequestRepository.BeginTransaction())
{
    certRequest.Oids.Clear();
    certRequestRepository.Delete(certRequest);
    transaction.Commit();
}