예제 #1
0
        public static void Save <TEntity>(TEntity entity)
        {
            With.Transaction(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));

                command.CommandText = GenerateUpdate <TEntity>();
                object value        = mapping.PrimaryKey.Value.GetValue(entity, null);
                AddParameter(command, "@id", value);
                int parameterIndex = 0;
                foreach (KeyValuePair <string, PropertyInfo> kvp in mapping.Columns)
                {
                    value = kvp.Value.GetValue(entity, null);
                    AddParameter(command, "@p" + parameterIndex, value);
                    parameterIndex += 1;
                }

                command.ExecuteNonQuery();
            });
        }
예제 #2
0
        public static void Create <TEntity>(TEntity entity)
        {
            With.Transaction(delegate(IDbCommand command)
            {
                Mapping mapping = GetMappingFor(typeof(TEntity));

                command.CommandText = GenerateCreate <TEntity>();

                int parameterIndex = 0;
                foreach (KeyValuePair <string, PropertyInfo> kvp in mapping.Columns)
                {
                    object value = kvp.Value.GetValue(entity, null);
                    AddParameter(command, "@p" + parameterIndex, value);
                    parameterIndex += 1;
                }

                object obj = command.ExecuteScalar();
                mapping.PrimaryKey.Value.SetValue(entity,
                                                  Convert.ChangeType(obj, mapping.PrimaryKey.Value.PropertyType), null);
            });
        }