private void InitializeDefaultGetIdFunction()
        {
            var idProp = typeof(TEntity).GetProperty("Id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                         ?? typeof(TEntity).GetProperty(typeof(TEntity).Name + "Id", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

            if (idProp == null)
            {
                throw new InvalidOperationException("Couldn't automatically determine the entity key. Specify a 'get ID' function before using Find() using SetIdFunction() extension method.");
            }

            var entityParam = Expression.Parameter(typeof(TEntity));
            var getIdLambda = Expression.Lambda(typeof(GetIdFunction <TEntity>),
                                                Expression.Convert(Expression.MakeMemberAccess(entityParam, idProp), typeof(object)),
                                                entityParam);

            this.GetIdFunction = (GetIdFunction <TEntity>)getIdLambda.Compile();
        }
Beispiel #2
0
 /// <summary>
 /// Sets the function that can return an entity's ID to the mock DbSet.
 /// </summary>
 /// <typeparam name="TEntity">The type of the entity</typeparam>
 /// <param name="dbSet">The mock DbSet.</param>
 /// <param name="getIdFunction">The function that can return an entity's ID.</param>
 /// <returns>The mock DbSet.</returns>
 public static IDbSet <TEntity> SetIdFunction <TEntity>(this IDbSet <TEntity> dbSet, GetIdFunction <TEntity> getIdFunction) where TEntity : class
 {
     ((MockDbSet <TEntity>)dbSet).GetIdFunction = getIdFunction;
     return(dbSet);
 }
Beispiel #3
0
 public Task <BigInteger> GetIdQueryAsync(GetIdFunction getIdFunction, BlockParameter blockParameter = null)
 {
     return(ContractHandler.QueryAsync <GetIdFunction, BigInteger>(getIdFunction, blockParameter));
 }