예제 #1
0
        /// <summary>
        /// Insert a ForeignKeyField record into the metaFields table.
        /// </summary>
        /// <param name="field">Foreign Key field of a Unique Key.</param>
        /// <returns>Returns the Id of the last ForeignKeyField added.</returns>
        public int CreateField(ForeignKeyField field)
        {
            try
            {
                #region InputValidation
                if (field == null)
                {
                    throw new ArgumentNullException("ForeignKeyField");
                }
                #endregion

                Query insertQuery = db.CreateQuery("insert into metaFields([DataTableName], [ViewId], [UniqueId], [FieldTypeId], [Name]) " +
                    "values (@DataTableName, @ViewId, @UniqueId, @FieldTypeId, @Name)");

                insertQuery.Parameters.Add(new QueryParameter("@DataTableName", DbType.String, field.TableName));
                insertQuery.Parameters.Add(new QueryParameter("@ViewId", DbType.Int32, field.GetView().Id));
                insertQuery.Parameters.Add(new QueryParameter("@UniqueId", DbType.Guid, field.UniqueId));
                insertQuery.Parameters.Add(new QueryParameter("@FieldTypeId", DbType.Int32, (int)field.FieldType));
                insertQuery.Parameters.Add(new QueryParameter("@Name", DbType.String, field.Name));

                db.ExecuteNonQuery(insertQuery);
                return GetMaxFieldId(field.GetView().Id);
            }
            catch (Exception ex)
            {
                throw new GeneralException("Could not create ForeignKey field in the database", ex);
            }
            finally
            {
            }
        }
예제 #2
0
        /// <summary>
        /// Create Foreign Key field.
        /// </summary>
        /// <param name="field">Foreign Key field to create.</param>
        /// <returns>Id of the newly created ForeignKey field.</returns>
        public int CreateField(ForeignKeyField field)
        {
            try
            {
                #region InputValidation
                if (field == null)
                {
                    throw new ArgumentNullException("ForeignKeyField");
                }
                #endregion

                XmlDocument xmlDoc = GetXmlDocument();
                XmlNode fieldsNode = GetFieldsNode(field.ViewElement);
                View view = field.GetView();
                XmlElement fieldElement = xmlDoc.CreateElement("Field");

                XmlAttribute fieldIdAttribute = xmlDoc.CreateAttribute("FieldId");
                fieldIdAttribute.Value = view.GetFieldId(field.ViewElement).ToString();
                fieldElement.Attributes.Append(fieldIdAttribute);
                field.Id = Int32.Parse(fieldIdAttribute.Value);

                XmlAttribute fieldTypeId = xmlDoc.CreateAttribute("FieldTypeId");
                fieldTypeId.Value = field.FieldType.ToString();
                fieldElement.Attributes.Append(fieldTypeId);

                XmlAttribute fieldNameAttribute = xmlDoc.CreateAttribute("Name");
                fieldNameAttribute.Value = field.Name;
                fieldElement.Attributes.Append(fieldNameAttribute);

                XmlAttribute fieldViewId = xmlDoc.CreateAttribute("ViewId");
                fieldViewId.Value = view.Id.ToString();
                fieldElement.Attributes.Append(fieldViewId);

                XmlAttribute dataTableName = xmlDoc.CreateAttribute("DataTableName");
                dataTableName.Value = field.TableName;
                fieldElement.Attributes.Append(dataTableName);

                fieldsNode.AppendChild(fieldElement);
                view.Project.Save();
                return field.Id;
            }
            finally
            {
            }
        }