예제 #1
0
        public bool Insert(TEntity entity)
        {
            var sql   = sqlGenerator.Insert();
            var newId = dataContext.ExecuteScalar <TEntity>(sql, entity.ToSqlParameters <TEntity>());

            var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>();

            var propMapping = mapping?.PropertyMappings.FirstOrDefault(x => x.IsPrimaryKey);

            propMapping?.PropertyInfo.SetValue(entity, Convert.ChangeType(newId, propMapping.PropertyInfo.PropertyType), null);

            return(true);
        }
예제 #2
0
        public static List <SqlParameter> ToSqlParameters <TEntity>(this object source) where TEntity : class
        {
            var list = new List <SqlParameter>();

            var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>();

            foreach (var prop in source.GetType().GetProperties())
            {
                var propMapping = mapping.GetMappingForType(prop);

                if (propMapping.IsIgnored)
                {
                    continue;
                }

                list.Add(new SqlParameter(prop.Name, prop.GetValue(source, null)));
            }

            return(list);
        }
예제 #3
0
        public static List <TEntity> MapTo <TEntity>(this IDataReader dr) where TEntity : class, new()
        {
            var list = new List <TEntity>();

            while (dr.Read())
            {
                var obj = Activator.CreateInstance <TEntity>();
                try
                {
                    var mapping = EntityMappingEngine.GetMappingForEntity <TEntity>();

                    foreach (var prop in obj.GetType().GetProperties())
                    {
                        var propMapping = mapping.GetMappingForType(prop);

                        if (propMapping.IsIgnored)
                        {
                            continue;
                        }

                        var colName = prop.Name;

                        if (!HasColumn(dr, colName))
                        {
                            continue;
                        }

                        if (!Equals(dr[colName], DBNull.Value))
                        {
                            prop.SetValue(obj, dr[colName]);
                        }
                    }
                    list.Add(obj);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
            return(list);
        }
예제 #4
0
 public SqlCrudGenerator()
 {
     EntityMapping = EntityMappingEngine.GetMappingForEntity <TEntity>();
 }
예제 #5
0
 public LambdaToSql()
 {
     EntityMapping = EntityMappingEngine.GetMappingForEntity <TEntity>();
 }