protected IEnumerable <TModel> Implement <TModel>(IApplicationDbBase dataContext, RawSqlString QueryString, params object[] parameters) where TModel : new()
        {
            List <TModel> results = new List <TModel>();


            var concurrencyDetector = dataContext.Database.GetService <IConcurrencyDetector>();

            using (concurrencyDetector.EnterCriticalSection())
            {
                var rawSqlCommand = dataContext.Database
                                    .GetService <IRawSqlCommandBuilder>()
                                    .Build(QueryString.Format, parameters);

                var reader = rawSqlCommand.RelationalCommand
                             .ExecuteReader(
                    dataContext.Database.GetService <IRelationalConnection>(),
                    rawSqlCommand.ParameterValues
                    ).DbDataReader;

                List <PropertyInfo> properties = new List <PropertyInfo>();
                typeof(TModel).GetProperties().ForEach(a => { try { reader.GetOrdinal(a.Name); properties.Add(a); } catch { } });

                while (reader.Read())
                {
                    var item = new TModel();
                    properties.ForEach(prop => {
                        try {
                            var ordinal = reader.GetOrdinal(prop.Name);
                            if (reader.IsDBNull(ordinal))
                            {
                                return;
                            }
                            if (reader.GetValue(ordinal) == DBNull.Value)
                            {
                                return;
                            }
                            prop.SetValue(item, reader.GetValue(ordinal));
                        } catch (Exception e) {
                        }
                    });
                    results.Add(item);
                }
            }
            return(results);
        }
 protected int Implement(IApplicationDbBase dataContext, RawSqlString QueryString, params object[] parameters)
 => dataContext.Database.ExecuteSqlCommand(QueryString, parameters);
Exemplo n.º 3
0
 /// <summary>
 /// Construction method
 /// </summary>
 /// <param name="databaseFactory"></param>
 public RepositoryBase(IDatabaseFactory databaseFactory)
 {
     _databaseFactory = databaseFactory;
     _dataContext     = databaseFactory.GetDBContextByModel <T>();
     _dbset           = _dataContext.Set <T>();
 }