0

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

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 关系。有没有办法阻止这种情况?

谢谢

4

1 回答 1

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");  
        }
    }
}
于 2013-07-01T19:04:49.823 回答