/// <summary> /// Gets Count method from Queryable that should run with DbSet method to get count of a model in database. /// </summary> /// <param name="seederInfo">The seeder method information.</param> /// <returns>Count method from Queryable for the model that should seed.</returns> protected internal virtual MethodInfo?GetCountMethod(SeederInfo seederInfo) { return(typeof(Queryable) .GetMethods() .FirstOrDefault(x => x.Name == "Count" && x.GetParameters().Length == 1)? .MakeGenericMethod(seederInfo.SeederAttribute.Type)); }
/// <summary> /// Creates an instance from the class which seeder is defined in it. /// </summary> /// <param name="seederInfo">The seeder method information.</param> /// <returns>An instance of the class which seeder is defined in it.</returns> protected virtual object?CreateSeederInstance(SeederInfo seederInfo) { var seederConstructorInfo = GetConstructorInfo(seederInfo.Type); if (seederConstructorInfo == null) { return(null); } var seederConstructorParameters = CreateConstructorParameters(seederConstructorInfo); return(Activator.CreateInstance(seederInfo.Type, seederConstructorParameters)); }
/// <summary> /// Gets DbSet method of seeder model from DbContext. /// </summary> /// <param name="seederInfo">The seeder method information.</param> /// <returns>DbSet method of seeder model from DbContext.</returns> protected internal virtual MethodInfo?GetDbSetMethod(SeederInfo seederInfo) { return(DbContext.GetType().GetMethods() .FirstOrDefault(x => x.Name == "Set" && x.GetParameters().Length == 0)? .MakeGenericMethod(seederInfo.SeederAttribute.Type)); }
/// <summary> /// Invokes a non-async seeder method. /// </summary> /// <param name="seederInfo">The seeder method information.</param> /// <param name="seederInstance">An instance of the class which seeder is defined in it.</param> protected virtual void InvokeSeeder(SeederInfo seederInfo, object seederInstance) { seederInfo.MethodInfo.Invoke(seederInstance, null); }
/// <summary> /// Invokes an async seeder method. /// </summary> /// <param name="seederInfo">The seeder method information.</param> /// <param name="seederInstance">An instance of the class which seeder is defined in it.</param> protected virtual void InvokeAsyncSeeder(SeederInfo seederInfo, object seederInstance) { (seederInfo.MethodInfo.Invoke(seederInstance, parameters: null) as Task)?.Wait(); }