Exemplo n.º 1
0
        public static string GenerateCode(ClrClass @class, DbTable table, DbSchema schema)
        {
            if (@class == null)
            {
                throw new ArgumentNullException("class");
            }
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            var foreignKeyTables = ForeignKeyHelper.GetForeignKeyTables(table.Columns, schema);

            if (table.IsReadOnly)
            {
                return(GetAdapterReadonOnly(@class, table, foreignKeyTables));
            }
            var collectionType = ClrTypeHelper.GetCollectionType(@class.Properties);

            if (collectionType == null)
            {
                return(GetAdapter(@class, table, foreignKeyTables));
            }
            return(GetAdapterWithCollection(@class, table, foreignKeyTables, FindCollectionTable(schema, collectionType)));
        }
Exemplo n.º 2
0
        public static DbTable[] GetForeignKeyTables(IEnumerable <DbColumn> columns, DbSchema schema)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns");
            }
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            var foreignKeyTables = new List <DbTable>();

            foreach (var column in columns)
            {
                var foreignKey = column.DbForeignKey;
                if (foreignKey != null)
                {
                    var foreignKeyTable = FindTableByForeignKey(schema, foreignKey);
                    var collectionType  = ClrTypeHelper.GetCollectionType(DbTableConverter.ToClrClass(foreignKeyTable, schema.Tables).Properties);
                    if (collectionType == null)
                    {
                        var name   = foreignKeyTable.Name;
                        var exists = false;
                        foreach (var t in foreignKeyTables)
                        {
                            if (t.Name == name)
                            {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists)
                        {
                            foreignKeyTables.Add(foreignKeyTable);
                        }
                    }
                }
            }

            return(foreignKeyTables.ToArray());
        }