使用Fluent Nhibernate删除多对多关系

时间:2013-09-05 15:36:45

标签: c# nhibernate fluent-nhibernate fluent sharp-architecture

请帮帮我!我无法在Oid和CertRequest表中删除对象和依赖。

我跟着:

的Oid

公共课Oid     {         public virtual int Id {get;保护集; }
        public virtual int RowVersion {get;组; }
        public virtual string Code {get;组; }         public virtual string Name {get;组; }     }

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");
    }
}

删除代码:

certRequest.Oids.Clear();

_certRequestRepository.SaveOrUpdate(certRequest);

_certRequestRepository.Delete(certRequest);

什么都不做。

任何提示......

更新

对不起,我没有说我在方法上使用Sharp-Architecture和Transaction属性。

1 个答案:

答案 0 :(得分:0)

_certRequestRepositoryISession类型的对象吗?

如果是这样,您必须调用_certRequestRepository.Flush()将删除提交到数据库。

更好的是,使用交易。

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