过滤器错误地应用于子关系

时间:2013-07-01 17:47:15

标签: c# nhibernate fluent-nhibernate

我认为我遇到的问题是过滤器错误地应用于它不应该的关系。这是我的对象:

public enum Capability 
{
Create = 1,
Edit = 2,
Delete = 3
}

public class Role
{
public virtual int TenantId {get;set;
public virtual IList<Capability> Capabilities { get; set; }
}

这是一个映射覆盖:

public class RoleOverride : IAutoMappingOverride<Role>
{
    public void Override(AutoMapping<Role> mapping)
    {
        mapping.HasMany(x => x.Capabilities)
               .Cascade.All()
               .Table("RoleCapability")
               .Element("CapabilityId", e => e.Type<NHibernate.Type.EnumType<Capability>>())
               .AsBag()
               .Not.LazyLoad();
    }
}

这是我的过滤器:

public class FilterHasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
          instance.ApplyFilter<TenantFilter>("tenantid = :tid");
    }
}

public class TenantFilter : FilterDefinition
{
    public TenantFilter()
    {
        WithName("TenantFilter").AddParameter("tid", NHibernateUtil.String);
    }
}

现在,当我尝试加载我的Role对象时,会出现这样的问题:

SELECT capabiliti0_.RoleId as RoleId0_, capabiliti0_.CapabilityId as Capabili2_0_ 
FROM RoleCapability capabiliti0_ WHERE  capabiliti0_.tenantid = :TenantFilter.tid and capabiliti0_.RoleId=?

问题是TenantId不应该应用于RoleCapability关系。有没有办法阻止这个?

由于

1 个答案:

答案 0 :(得分:0)

这解决了我的问题:

public abstract class TenantEntity
{
public virtual int TenantId {get;set;}

}


public class Role : TenantEntity
{
public virtual int TenantId {get;set;
public virtual IList<Capability> Capabilities { get; set; }
}

public class FilterHasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        if (instance.ChildType.IsSubclassOf(typeof(TenantEntity)))
        {
                instance.ApplyFilter<TenantFilter>("tenantid = :tid");  
        }
    }
}