示例#1
0
        public static bool Delete(this IOrmModel me, int PrimaryKeyValue)
        {
            //Note, this returns true if no row is found matching the key... bubble exception maybe from SQL?
            KeyValuePair <string, int?> _PrimaryKey = ReflectionHelper.PrimaryKey(me);
            /*Move donot use reflection unless error*/
            string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me));

            _message = string.Format("Process {0} failed; {1}={2}", ReflectionHelper.GetProcessName(me), _PrimaryKey.Key, _PrimaryKey.Value);
            try {
                using (SqlConnection cn = new SqlConnection(Connstring())) {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand(me.OrmContext.Delete, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter(_PrimaryKey.Key, SqlDbType.Int));
                    cmd.Parameters[_PrimaryKey.Key].Direction = ParameterDirection.InputOutput;
                    cmd.Parameters[_PrimaryKey.Key].Value     = PrimaryKeyValue;
                    cmd.Parameters.AddWithValue("delete", true);
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
                OrmCaching.ClearCacheForProcessType(me.GetType().FullName);
                return(true);
            }
            catch (SqlException ex) {
                //err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex);
                throw;
            }
            catch (Exception ex) {
                //err.WriteError(_message, GetProcessName(me), ex);
            }
            return(false);
        }
示例#2
0
        public static IOrmModel Create(this IOrmModel me)
        {
            KeyValuePair <string, int?> _PrimaryKey = ReflectionHelper.PrimaryKey(me);

            /*Move donot use reflection unless error*/
            //string _message = string.Format("Process {0} failed;", ReflectionHelper.GetProcessName(me));
            //_message = string.Format("Process {0} failed; {1}={2}", ReflectionHelper.GetProcessName(me), _PrimaryKey.Key, _PrimaryKey.Value);
            try {
                using (SqlConnection cn = new SqlConnection(Connstring())) {
                    cn.Open();
                    SqlCommand cmd = new SqlCommand(me.OrmContext.Create, cn);
                    cmd.CommandType = CommandType.StoredProcedure;

                    foreach (DataAnnotationsModelMetadata field in ModelMetadataProviders.Current.GetMetadataForProperties(me, me.GetType()))
                    {
                        if (
                            field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM", true))
                            & !field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_Update", false))
                            )
                        {
                            if (field.AdditionalValues.Contains(new KeyValuePair <string, object>("ORM_PrimaryKey", true)))
                            {
                                cmd.Parameters.Add(new SqlParameter(field.PropertyName, SqlDbType.Int));
                                cmd.Parameters[field.PropertyName].Direction = ParameterDirection.Output;
                            }
                            else
                            {
                                System.Reflection.PropertyInfo PropInfo = me.GetType().GetProperty(field.PropertyName);
                                object PropVal = PropInfo.GetValue(me, null);
                                cmd.Parameters.AddWithValue(field.PropertyName, PropVal);
                            }
                        }
                    }
                    cmd.ExecuteNonQuery();
                    ReflectionHelper.SetProperty(
                        me
                        , _PrimaryKey.Key
                        , cmd.Parameters[_PrimaryKey.Key].Value
                        );
                    cn.Close();
                }
                OrmCaching.ClearCacheForProcessType(me.GetType().FullName);
            }
            catch (SqlException ex) {
                // err.WriteError("SQL Exception: " + _message, GetProcessName(me), ex);
                throw;
            }
            catch (Exception ex) {
                // err.WriteError(_message, GetProcessName(me), ex);
            }
            return(me);
        }