Exemplo n.º 1
0
        protected List <AssociationPropertyInfo> CreateAssoicationClass(TableName tname, Class clss)
        {
            List <AssociationPropertyInfo> properties = new List <AssociationPropertyInfo>();

            var schema = TableSchemaCache.GetSchema(tname);
            var pkeys  = schema.ByForeignKeys.Keys.OrderBy(k => k.FK_Table);

            foreach (IForeignKey pkey in pkeys)
            {
                string entity = ident.Identifier(pkey.FK_Table);

                TypeInfo type;
                string   propertyName;
                bool     one2many = IsOneToMany(tname, pkey);
                if (one2many)
                {
                    type = new TypeInfo {
                        UserType = $"EntitySet<{entity}>"
                    };
                    propertyName = Plural.Pluralize(entity);
                }
                else
                {
                    type = new TypeInfo {
                        UserType = $"EntityRef<{entity}>"
                    };
                    propertyName = Plural.Singularize(entity);
                }

                var property = new Property(type, propertyName);
                clss.Add(property);
                properties.Add(new AssociationPropertyInfo
                {
                    PropertyType = entity,
                    PropertyName = propertyName,
                    OneToMany    = one2many,
                    PK_Column    = pkey.PK_Column,
                    FK_Column    = pkey.FK_Column,
                });
            }

            var fkeys = schema.ForeignKeys.Keys.OrderBy(k => k.FK_Table);

            foreach (IForeignKey fkey in fkeys)
            {
                string   entity = ident.Identifier(fkey.PK_Table);
                TypeInfo type   = new TypeInfo {
                    UserType = $"EntityRef<{entity}>"
                };
                string propertyName = Plural.Singularize(entity);
                var    property     = new Property(type, propertyName);
                clss.Add(property);

                properties.Add(new AssociationPropertyInfo
                {
                    PropertyType = entity,
                    PropertyName = propertyName,
                    OneToMany    = false,
                    PK_Column    = fkey.FK_Column,
                    FK_Column    = fkey.PK_Column,
                });
            }

            return(properties);
        }
Exemplo n.º 2
0
        /// <summary>
        /// add children tables
        /// </summary>
        /// <param name="clss"></param>
        /// <param name="constructor"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        private Property AddEntitySet(Class clss, Constructor constructor, IForeignKey key)
        {
            TableName fk_tname = new TableName(tname.DatabaseName, key.FK_Schema, key.FK_Table);
            string    fk_cname = fk_tname.ToClassName(rule: null);
            string    pname;

            Property prop;
            TypeInfo ty;
            Field    field;

            var fk_schema = TableSchemaCache.GetSchema(fk_tname);
            var _keys     = fk_schema.PrimaryKeys.Keys;

            if (_keys.Length == 1 && _keys.Contains(key.FK_Column))
            {
                // 1:1 mapping
                pname = clss.MakeUniqueName(Plural.Singularize(fk_cname));
                ty    = new TypeInfo {
                    UserType = $"EntityRef<{fk_cname}>"
                };
                field = new Field(ty, $"_{pname}")
                {
                    Modifier = Modifier.Private
                };

                prop = new Property(new TypeInfo {
                    UserType = fk_cname
                }, pname)
                {
                    Modifier = Modifier.Public
                };
                prop.Gets.Append($"return this._{pname}.Entity;");
                prop.Sets.Append($"this._{pname}.Entity = value;");

                prop.AddAttribute(new AttributeInfo("Association",
                                                    new
                {
                    Name         = $"{this.ClassName}_{fk_cname}",
                    Storage      = $"_{pname}",
                    ThisKey      = key.PK_Column,
                    OtherKey     = key.FK_Column,
                    IsUnique     = true,
                    IsForeignKey = false
                }));
            }
            else
            {
                //1:n mapping
                pname = clss.MakeUniqueName(Plural.Pluralize(fk_cname));
                constructor.Statement.AppendLine($"this._{pname} = new EntitySet<{fk_cname}>();");

                ty = new TypeInfo {
                    UserType = $"EntitySet<{fk_cname}>"
                };
                field = new Field(ty, $"_{pname}")
                {
                    Modifier = Modifier.Private
                };

                prop = new Property(ty, pname)
                {
                    Modifier = Modifier.Public
                };
                prop.Gets.Append($"return this._{pname};");
                prop.Sets.Append($"this._{pname}.Assign(value);");

                prop.AddAttribute(new AttributeInfo("Association",
                                                    new
                {
                    Name         = $"{this.ClassName}_{fk_cname}",
                    Storage      = $"_{pname}",
                    ThisKey      = key.PK_Column,
                    OtherKey     = key.FK_Column,
                    IsForeignKey = false
                }));
            }

            clss.Add(field);


            return(prop);
        }