示例#1
0
        /// <summary>
        /// Writes the C# code of the table
        /// </summary>
        /// <returns></returns>
        public string Write()
        {
            var codeTarget = _codeWriterSettings.CodeTarget;

            _dataAnnotationWriter = new DataAnnotationWriter(IsEntityFramework(), _codeWriterSettings);
            var className = _table.NetName;

            if (string.IsNullOrEmpty(className) && _table.DatabaseSchema != null)
            {
                PrepareSchemaNames.Prepare(_table.DatabaseSchema, _codeWriterSettings.Namer);
                className = _table.NetName;
            }
            _dataTypeWriter.CodeTarget = codeTarget;

            _inheritanceTable = _table.FindInheritanceTable();

            WriteNamespaces();

            if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace))
            {
                _cb.BeginNest("namespace " + _codeWriterSettings.Namespace);
            }

            if (codeTarget == CodeTarget.PocoRiaServices)
            {
                WriteRiaClass(className);
            }
            else
            {
                var tableOrView     = _table is DatabaseView ? "view" : "table";
                var comment         = "Class representing " + _table.Name + " " + tableOrView;
                var classDefinition = "public class " + className;
                if (_inheritanceTable != null)
                {
                    classDefinition += " : " + _inheritanceTable.NetName;
                }

                using (_cb.BeginNest(classDefinition, comment))
                {
                    WriteClassMembers(className);
                }
            }

            if (_table.HasCompositeKey && _inheritanceTable == null)
            {
                WriteCompositeKeyClass(className);
            }

            if (!string.IsNullOrEmpty(_codeWriterSettings.Namespace))
            {
                _cb.EndNest();
            }

            return(_cb.ToString());
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeWriter"/> class.
        /// </summary>
        /// <param name="schema">The schema.</param>
        /// <param name="codeWriterSettings">The code writer settings.</param>
        public CodeWriter(DatabaseSchema schema, CodeWriterSettings codeWriterSettings)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }
            if (codeWriterSettings == null)
            {
                throw new ArgumentNullException("codeWriterSettings");
            }

            _schema             = schema;
            _codeWriterSettings = codeWriterSettings;

            PrepareSchemaNames.Prepare(schema, codeWriterSettings.Namer);
        }