Exemplo n.º 1
0
        public virtual void Delete(LinqDBDataContext dc)
        {
            Type t = this.GetType();

            object[] oo = t.GetCustomAttributes(typeof(TableAttribute), true);
            if (oo == null || oo.Length < 1)
            {
                return;
            }
            TableAttribute ta = (TableAttribute)oo[0];

            string        q;
            List <object> Params   = new List <object>();
            PropertyInfo  PiStatus = t.GetProperty("Status");

            if (PiStatus != null && PiStatus.PropertyType.FullName == typeof(bool?).FullName)
            {
                q = "UPDATE " + ta.Name + " SET Status=0, Updated={0} WHERE ";
                Params.Add(DateTime.Now);
            }
            else
            {
                q = "DELETE " + ta.Name + " WHERE ";
            }

            PropertyInfo[] pis        = t.GetProperties();
            bool           FirstWhere = true;

            foreach (PropertyInfo pi in pis)
            {
                oo = pi.GetCustomAttributes(typeof(ColumnAttribute), true);
                if (oo == null || oo.Length < 1)
                {
                    continue;
                }
                ColumnAttribute ca = (ColumnAttribute)oo[0];
                if (!ca.IsPrimaryKey)
                {
                    continue;
                }

                object v;
                if (pi.Name == "InstanceId")
                {
                    v = LinqMicajahDataContext.InstanceId;
                }
                else
                {
                    v = pi.GetValue(this, null);
                }
                Params.Add(v);
                q += (FirstWhere ? "" : " AND ") + ca.Name + "={" + (Params.Count - 1) + "} ";

                FirstWhere = false;
            }

            dc.ExecuteCommand(q, Params.ToArray());
        }