筛选器未正确应用于子关系
本文关键字:应用于 关系 筛选 | 更新日期: 2023-09-27 18:20:32
我认为我遇到了一个问题,一个过滤器被错误地应用于一个不应该应用的关系。以下是我的对象:
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);
}
}
现在,当我试图加载我的角色对象时,它执行此操作时出现了一个问题:
SELECT capabiliti0_.RoleId as RoleId0_, capabiliti0_.CapabilityId as Capabili2_0_
FROM RoleCapability capabiliti0_ WHERE capabiliti0_.tenantid = :TenantFilter.tid and capabiliti0_.RoleId=?
问题是TenantId不应应用于RoleCapability关系。有什么办法阻止这种情况吗?
感谢
这解决了我的问题:
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");
}
}
}