C# 未为类型';System.Int32';和';System.String';正在运行的筛选器Asp.net核心5

C# 未为类型';System.Int32';和';System.String';正在运行的筛选器Asp.net核心5,c#,asp.net-core,action-filter,custom-action-filter,C#,Asp.net Core,Action Filter,Custom Action Filter,我正在从事一个Asp.net核心5项目。 我创建了一个操作过滤器,该过滤器将应用于HttPost操作,尤其是create,该过滤器将检查数据库中是否已经存在具有从模型提交的相同值的对象 我如何使用它: 过滤器应接收要签入的实体,以及要在实体中签入的属性名称,以及存在记录时显示的错误消息 过滤器定义: 我觉得有点复杂 public class RecordShouldNotExistFilterAttribute:TypeFilterAttribute { public RecordSh

我正在从事一个
Asp.net核心5
项目。 我创建了一个
操作过滤器
,该过滤器将应用于
HttPost
操作,尤其是
create
,该过滤器将检查数据库中是否已经存在具有从
模型提交的相同值的对象

我如何使用它: 过滤器应接收要签入的
实体
,以及要在
实体
中签入的属性名称,以及存在记录时显示的错误消息

过滤器定义: 我觉得有点复杂

public class RecordShouldNotExistFilterAttribute:TypeFilterAttribute
{

    public RecordShouldNotExistFilterAttribute( Type entityType, string errorMessage= "Existe déjà", params string[] propertiesToBeChecked) : base( typeof(RecordShouldNotExistFilter<>).MakeGenericType(entityType))
    {
        Arguments = new object[] {errorMessage , propertiesToBeChecked};
    }

}

public class RecordShouldNotExistFilter<TEntity>:IActionFilter where  TEntity : class
{

    private readonly AppDbContext _dbContext;
    public           string[]     PropertiesToBeChecked { get; set; }
    public           string       ErrorMessage          { get; set; }

    public RecordShouldNotExistFilter( AppDbContext dbContext ,string errorMessage,params string[] propertiesToBeChecked)
    {
        _dbContext            = dbContext;
        PropertiesToBeChecked = propertiesToBeChecked;
        ErrorMessage          = errorMessage;
    }

    public void OnActionExecuting( ActionExecutingContext context )
    {

        Expression queryExpression = null;

        context.ActionArguments.TryGetValue( "model" , out var model );

        var param = Expression.Parameter(typeof(TEntity));

        foreach ( var property in PropertiesToBeChecked )
        {
            var propValue = model?.GetType().GetProperty( property )?.GetValue( model );

            var equalExpression = Expression.Equal( left : Expression.Property( param , property) , right : Expression.Constant( propValue ) );

            queryExpression = queryExpression == null ? equalExpression : Expression.And( queryExpression , equalExpression );
        }

        var condition = Expression.Lambda<Func<TEntity , bool>>( queryExpression , param );

        var result = _dbContext.Set<TEntity>().Where( condition );

        if ( result.Any() )
        {
            context.Result = RedirectToActionResult( action : "ExistError" , controller : "/Exception" ,errorModel: new ErrorModel()
                                                                                                         {
                                                                                                             Title       = "Existe déjà" ,
                                                                                                             Description = $"{ErrorMessage}" ,
                                                                                                             ReturnUrl   = "/Dashboard"
                                                                                                         } );
        }


    }

    public void OnActionExecuted( ActionExecutedContext   context )
    {
        
    }

    #region Private methods

    private static RedirectToActionResult RedirectToActionResult(string action, string controller, ErrorModel errorModel)
    {
        return new RedirectToActionResult(action, controller, errorModel);
    }

    #endregion

}
问题:

[RecordShouldNotExistFilter(entityType: typeof(PedagogicalSequence), errorMessage: "A record with the same ID already exists",propertiesToBeChecked: nameof(Branche.Id))]
    public IActionResult Create(Branche model)
    {
    //Some logic here
    }
问题出在
操作中
我遇到了这个错误 InvalidOperationException:未定义二进制运算符Equal 对于类型“System.Int32”和“System.String”。 System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType、字符串opName、表达式左、表达式右、bool liftToNull)

错误屏幕:

请提供任何帮助以解决此问题?

在我的情况下,请单击此处:

[RecordShouldNotExistFilter(entityType: typeof(PedagogicalSequence), errorMessage: "A record with the same ID already exists",propertiesToBeChecked: nameof(Branche.Id))]
我发送了一个错误的实体


我更改了它,一切正常

参数是否等于相同的类型?是的,从
模型中提交的
Id
的值是
string
,并且他的类型已经
string
和正在检查的属性,表示DB项的实体也是一个字符串?(EducationalSequence.Id是一个字符串?)哦,该死的,我的上帝你说得对,我发送了一个
typeof()
用于错误的实体谢谢你兄弟,再好不过一点硬纸板分析了!不客气!:)