private bool FilterIneligible(DatabaseTable table)
 {
     if (!IsCodeFirst()) return false;
     if (table.IsManyToManyTable()  && _codeWriterSettings.CodeTarget == CodeTarget.PocoEntityCodeFirst)
         return true;
     if (table.PrimaryKey == null)
         return true;
     if (table.Name.Equals("__MigrationHistory", StringComparison.OrdinalIgnoreCase)) //EF 6
         return true;
     if (table.Name.Equals("__EFMigrationsHistory", StringComparison.OrdinalIgnoreCase)) //EF Core1
         return true;
     if (table.Name.Equals("EdmMetadata", StringComparison.OrdinalIgnoreCase))
         return true;
     return false;
 }
 private bool FilterIneligible(DatabaseTable table)
 {
     if (!IsCodeFirst()) return false;
     if (table.IsManyToManyTable())
         return true;
     if (table.PrimaryKey == null)
         return true;
     if (table.Name.Equals("__MigrationHistory", StringComparison.OrdinalIgnoreCase))
         return true;
     if (table.Name.Equals("EdmMetadata", StringComparison.OrdinalIgnoreCase))
         return true;
     return false;
 }
        private void WriteForeignKeyCollection(DatabaseTable foreignKeyChild)
        {
            if (foreignKeyChild.IsManyToManyTable())
            {
                WriteManyToManyForeignKeyCollection(foreignKeyChild);
                return;
            }

            var foreignKeyTable = foreignKeyChild.Name;
            var childClass = foreignKeyChild.NetName;
            var foreignKey = foreignKeyChild.ForeignKeys.FirstOrDefault(fk => fk.RefersToTable == _table.Name);
            if (foreignKey == null) return; //corruption in our database
            //we won't deal with composite keys
            if (_table.IsSharedPrimaryKey(foreignKeyChild))
            {
                _cb.AppendFormat("//shared primary key to {0} ({1})", foreignKeyTable, childClass);
                _cb.AppendFormat("HasOptional(x => x.{0});", childClass);
                return;
            }

            var fks = _table.InverseForeignKeys(foreignKeyChild);
            foreach (var fk in fks)
            {
                _cb.AppendFormat("//Foreign key to {0} ({1})", foreignKeyTable, childClass);
                var propertyName = _codeWriterSettings.Namer.ForeignKeyCollectionName(_table.Name, foreignKeyChild, fk);
                //specify the opposite direction? Probably not needed
                _cb.AppendFormat("HasMany(x => x.{0});", propertyName);

            }
        }
        private void WriteForeignKeyCollection(DatabaseTable foreignKeyChild)
        {
            if (foreignKeyChild.IsManyToManyTable() && _codeWriterSettings.CodeTarget == CodeTarget.PocoEntityCodeFirst)
            {
                WriteManyToManyForeignKeyCollection(foreignKeyChild);
                return;
            }

            var foreignKeyTable = foreignKeyChild.Name;
            var childClass = foreignKeyChild.NetName;
            var foreignKey = foreignKeyChild.ForeignKeys.FirstOrDefault(fk => fk.RefersToTable == _table.Name);
            if (foreignKey == null) return; //corruption in our database
            //we won't deal with composite keys
            if (_table.IsSharedPrimaryKey(foreignKeyChild))
            {
                _cb.AppendFormat("//shared primary key to {0} ({1})", foreignKeyTable, childClass);
                if (_codeWriterSettings.CodeTarget == CodeTarget.PocoEntityCodeFirst)
                {
                    _cb.AppendFormat(Builder + "HasOptional(x => x.{0});", childClass);
                }
                else
                {
                    _cb.AppendFormat(Builder + "HasOne(x => x.{0}).WithOne();", childClass);
                }
                return;
            }

            //the foreign keys that point at this table from the other table
            var fks = _table.InverseForeignKeys(foreignKeyChild);
            foreach (var fk in fks)
            {
                _cb.AppendFormat("//Foreign key to {0} ({1})", foreignKeyTable, childClass);
                var propertyName = _codeWriterSettings.Namer.ForeignKeyCollectionName(_table.Name, foreignKeyChild, fk);
                if (_codeWriterSettings.CodeTarget == CodeTarget.PocoEntityCodeFirst)
                {
                    //specify the opposite direction? Probably not needed
                    _cb.AppendFormat("HasMany(x => x.{0});", propertyName);
                }
                else
                {
                    //EF Core v1 - inverse direction is required in Core
                    var dependentPropertyName = _codeWriterSettings.Namer.ForeignKeyName(foreignKeyChild, fk);

                    _cb.AppendFormat("b.HasMany(x => x.{0}).WithOne(d => d.{1});", propertyName, dependentPropertyName);
                }
            }
        }