tmsmith / Dapper-Extensions

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs. For more advanced querying scenarios, Dapper Extensions provides a predicate system. The goal of this library is to keep your POCOs pure by not requiring any attributes or base class inheritance.
1.79k stars 586 forks source link

This operation is only valid on generic types. #282

Open dradoaica opened 2 years ago

dradoaica commented 2 years ago

Exception:

This operation is only valid on generic types. at System.RuntimeType.GetGenericTypeDefinition() at DapperExtensions.DapperImplementor.AddParameter[T](T entity, DynamicParameters parameters, IMemberMap prop, Boolean useColumnAlias) at DapperExtensions.DapperImplementor.GetDynamicParameters[T](T entity, IClassMapper classMap, IList1 sequenceColumn, IList1 foreignKeys, IList1 ignoredColumns, Boolean useColumnAlias) at DapperExtensions.DapperImplementor.GetDynamicParameters[T](IClassMapper classMap, T entity, Boolean useColumnAlias) at DapperExtensions.DapperImplementor.InternalInsert[T](IDbConnection connection, T entity, IDbTransaction transaction, Nullable1 commandTimeout, IClassMapper classMap, IList1 nonIdentityKeyProperties, IMemberMap identityColumn, IMemberMap triggerIdentityColumn, IList1 sequenceIdentityColumn) at DapperExtensions.DapperAsyncImplementor.InternalInsertAsync[T](IDbConnection connection, T entity, IDbTransaction transaction, Nullable1 commandTimeout, IClassMapper classMap, IList1 nonIdentityKeyProperties, IMemberMap identityColumn, IMemberMap triggerIdentityColumn, IList1 sequenceIdentityColumn) at DapperExtensions.DapperAsyncImplementor.InsertAsync[T](IDbConnection connection, T entity, IDbTransaction transaction, Nullable1 commandTimeout)

In DapperImplementor:

if (prop.MemberInfo.DeclaringType == typeof(bool) || (prop.MemberInfo.DeclaringType.IsGenericType && prop.MemberType.GetGenericTypeDefinition() == typeof(Nullable<>) && prop.MemberInfo.DeclaringType.GetGenericArguments()[0] == typeof(bool))) { ... }

There should be a check prop.MemberType.IsGenericType before prop.MemberType.GetGenericTypeDefinition()

valfrid-ly commented 2 years ago

Could you please provide more information to help replicating it?

dradoaica commented 2 years ago

List<AlertExecution> ae = new List<AlertExecution>(); ... using (Microsoft.Data.SqlClient.SqlConnection connection = ...) { await connection.InsertAsync(ae.AsEnumerable()); }

where AlertExecution : FullAuditedEntity<Guid> and has a member of generic type

nroda1976 commented 1 year ago

Using Dapper 2.0.123 and DapperExtensions 1.7 A generic error with be raise in DapperImplementor in the lines reported. The code below work fine in version 1.6.3 but raise the reported issue in 1.7 How to Reproduce example:

public class RepositoryBase<TEntity> where TEntity : class
{
    public DateTime CreationDate { get; set; }
        public string CreationUser { get; set; }
}

public class UserLogin : RepositoryBase<UserLogin>
{
    [Key]
    public long UserID { get; set; }
    public long EntityID { get; set; }

    public static UserLogin Login()
    {
        using (SqlConnection cn = new SqlConnection(XXXXX))
        {
            cn.Update(userLogin);
        }
        return userLogin;
    }
}