Esempio n. 1
0
 private void WritePrimaryKey(string className)
 {
     if (_table.HasCompositeKey)
     {
         if (!IsEntityFramework())
         {
             _cb.AppendAutomaticProperty(className + "Key", "Key");
         }
         else
         {
             //code first composite key
             foreach (var column in _table.Columns.Where(c => c.IsPrimaryKey))
             {
                 WriteColumn(column, false);
             }
         }
     }
     else
     {
         //single primary key column
         var column = _table.PrimaryKeyColumn;
         //could be a view or have no primary key
         if (column != null)
         {
             WriteColumn(column);
         }
     }
 }
Esempio n. 2
0
        private void WriteClassMembers(string className)
        {
            if (_codeWriterSettings.CodeTarget == CodeTarget.PocoRiaServices)
            {
                RiaServicesWriter.WritePrivateConstructor(className, _cb);
            }
            else
            {
                InitializeCollectionsInConstructor(className);
            }

            if (_table.HasCompositeKey)
            {
                if (!IsEntityFramework())
                {
                    _cb.AppendAutomaticProperty(className + "Key", "Key");
                }
                else
                {
                    //code first composite key
                    foreach (var column in _table.Columns.Where(c => c.IsPrimaryKey))
                    {
                        WriteColumn(column);
                    }
                }
            }

            foreach (var column in _table.Columns)
            {
                if (_table.HasCompositeKey && column.IsPrimaryKey)
                {
                    continue;
                }
                WriteColumn(column);
            }

            // KL: Writing out all foreign keys on their own. Needed for composite foreign keys.
            foreach (var fKey in _table.ForeignKeys)
            {
                WriteForeignKey(fKey);
            }

            WriteForeignKeyCollections();

            if (!_table.HasCompositeKey && _codeWriterSettings.CodeTarget != CodeTarget.PocoRiaServices)
            {
                var overrider = new OverrideWriter(_cb, _table);
                overrider.AddOverrides();
            }
        }
Esempio n. 3
0
        private void WriteForeignKeyCollectionProperties()
        {
            var listType = "IEnumerable<";

            var hasTablePerTypeInheritance =
                (table.ForeignKeyChildren.Count(fk => table.IsSharedPrimaryKey(fk)) > 1);

            foreach (var foreignKey in table.ForeignKeyChildren)
            {
                if (foreignKey.IsManyToManyTable() && codeWriterSettings.CodeTarget == CodeTarget.PocoEntityCodeFirst)
                {
                    WriteManyToManyCollection(foreignKey);
                    continue;
                }
                if (table.IsSharedPrimaryKey(foreignKey))
                {
                    if (hasTablePerTypeInheritance)
                    {
                        continue;
                    }
                    //type and property name are the same
                    classBuilder.AppendAutomaticProperty(foreignKey.NetName, foreignKey.Name, true);
                    continue;
                }

                //the other table may have more than one fk pointing at this table
                var fks = table.InverseForeignKeys(foreignKey);
                foreach (var fk in fks)
                {
                    var propertyName = codeWriterSettings.Namer.ForeignKeyCollectionName(table.Name, foreignKey, fk);
                    var dataType     = listType + foreignKey.NetName + ">";
                    WriteForeignKeyChild(propertyName, dataType);
                    classBuilder.AppendLine("");
                }
            }
        }