Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow 全局过滤器子查询 #10

Closed
YeRenJie opened this issue Jan 20, 2022 · 1 comment
Closed

Stack overflow 全局过滤器子查询 #10

YeRenJie opened this issue Jan 20, 2022 · 1 comment

Comments

@YeRenJie
Copy link

using System;
using System.Threading.Tasks;
using FreeSql;
using FreeSql.DataAnnotations;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var freeSqlBuilder = new FreeSqlBuilder()
.UseConnectionString(DataType.Sqlite, "Data Source =| DataDirectory |\admindb.db; Pooling = true; Min Pool Size = 1", typeof(FreeSql.Sqlite.SqliteProvider<>))
.UseAutoSyncStructure(false)
.UseLazyLoading(false)
.UseNoneCommandParameter(true);

        var fsql = freeSqlBuilder.Build();
        fsql.GlobalFilter.Apply<IOrganization>("Organization", a => fsql.Select<UserOrganization>().Where(b=>b.UserId==1&&b.OrganizationId==a.OrganizationId).Any());

        Console.WriteLine(fsql.Select<UserEntity>().ToSql());
        Console.WriteLine();
    
    }
 
    public interface IOrganization { 
    long OrganizationId { get; set; }
    }
    public class UserOrganization: IOrganization
    { 
    
        public long UserId { get; set; }
        public long OrganizationId { get; set; }
    }
    /// <summary>
    /// 用户
    /// </summary>
    [Table(Name = "ad_user")]
    public class UserEntity :IOrganization
    {
        /// <summary>
        /// 主键Id
        /// </summary>
        [Column(Position = 1, IsIdentity = true, IsPrimary = true)]
        public virtual long Id { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        [Column(StringLength = 60)]
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [Column(StringLength = 60)]
        public string Password { get; set; }
        public long OrganizationId { get; set; }
    }
}

}

@2881099
Copy link
Owner

2881099 commented Jan 20, 2022

原因:

UserOrganization : IOrganization 这个关系导致的死循环,过滤器子层,又会去做过滤,过滤条件又是 UserOrganization,然后一直向下循环做过滤条件。

解决:

在过滤器条件中的子查询中,临时禁用过滤器,如下:

fsql.GlobalFilter.Apply<IOrganization>("Organization", a => 
    fsql.Select<UserOrganization>()
        .DisableGlobalFilter("Organization")
        .Where(b => b.UserId == 1 && b.OrganizationId == a.OrganizationId).Any()
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants