Exemple #1
0
        public virtual TEntity GetAndCreate(TableSchema tableSchema)
        {
            if (_content == null)
            {
                throw new ArgumentNullException("tableSchema");
            }

            TEntity entity = EntityContext.Entities.ByTable(tableSchema.FullName);

            if (entity != null)
            {
                return(entity);
            }

            entity = new TEntity
            {
                FullName    = tableSchema.FullName,
                TableName   = tableSchema.Name,
                TableSchema = tableSchema.Owner
            };

            string className = _content.ToClassName(tableSchema.Name);

            className = _content.UniqueNamer.UniqueClassName(className);

            string mappingName = className + "Map";

            mappingName = _content.UniqueNamer.UniqueClassName(mappingName);

            string contextName = _content.Settings.ContextName(className);

            contextName = _content.ToPropertyName(EntityContext.ClassName, contextName);
            contextName = _content.UniqueNamer.UniqueContextName(contextName);

            entity.ClassName   = className;
            entity.ContextName = contextName;
            entity.MappingName = mappingName;

            _propertyTemplate.Entity = entity;

            foreach (ColumnSchema dataObjectBase in tableSchema.Columns.Selected())
            {
                // 过滤掉数据库自定义类型
                if (dataObjectBase.NativeType.Equals("hierarchyid", StringComparison.OrdinalIgnoreCase) ||
                    dataObjectBase.NativeType.Equals("sql_variant", StringComparison.OrdinalIgnoreCase))
                {
                    Debug.WriteLine("跳过字段 '{0}',因为目前不支持自定义类型'{1}'的处理.", dataObjectBase.Name, dataObjectBase.NativeType);
                    continue;
                }
                TProperty property = _propertyTemplate.GetAndCreate(dataObjectBase);
                entity.Properties.Add(property);
            }
            entity.Properties.IsProcessed = true;
            EntityContext.Entities.Add(entity);
            return(entity);
        }
        public TEntity GetAndCreate(ViewSchema viewSchema)
        {
            if (_content == null)
            {
                throw new ArgumentNullException("viewSchema");
            }

            TEntity entity = EntityContext.Entities.ByTable(viewSchema.FullName);

            if (entity != null)
            {
                return(entity);
            }

            entity = new TEntity
            {
                FullName    = viewSchema.FullName,
                TableName   = viewSchema.Name,
                TableSchema = viewSchema.Owner
            };

            string className = _content.ToClassName(viewSchema.Name);

            className = _content.UniqueNamer.UniqueClassName(className);

            string mappingName = className + "Map";

            mappingName = _content.UniqueNamer.UniqueClassName(mappingName);

            string contextName = _content.Settings.ContextName(className);

            contextName = _content.ToPropertyName(EntityContext.ClassName, contextName);
            contextName = _content.UniqueNamer.UniqueContextName(contextName);

            entity.ClassName   = className;
            entity.ContextName = contextName;
            entity.MappingName = mappingName;

            entity.Properties = new PropertyCollection <TProperty>();

            PropertyTemplate <TEntity, TProperty> propertyTemplate = new PropertyTemplate <TEntity, TProperty>(_content)
            {
                Entity = entity
            };

            foreach (ViewColumnSchema dataObjectBase in viewSchema.Columns.Selected())
            {
                propertyTemplate.GetAndCreate(dataObjectBase);
            }

            EntityContext.Entities.Add(entity);

            return(entity);
        }
Exemple #3
0
        public override Relationship GetAndCreate(TableKeySchema tableKeySchema)
        {
            //获取主键表对应实体
            TEntity primaryEntity = EntityContext.Entities.ByTable(tableKeySchema.PrimaryKeyTable.FullName);
            //主表类名
            string primaryName = primaryEntity.ClassName;
            //外键表名
            string foreignName = ForeignEntity.ClassName;
            //映射名称
            string relationshipName = tableKeySchema.Name;

            //获取唯一映射名称
            relationshipName = _content.UniqueNamer.UniqueRelationshipName(relationshipName);
            //判断是否级联删除
            bool isCascadeDelete = _content.IsCascadeDelete(tableKeySchema);
            bool foreignMembersRequired;
            bool primaryMembersRequired;
            //获取外键表所有键属性名称
            List <string> foreignMembers = _content.GetKeyMembers(ForeignEntity, tableKeySchema.ForeignKeyMemberColumns, tableKeySchema.Name, out foreignMembersRequired);
            //获取主表中所有键的成员属性名称
            List <string> primaryMembers = _content.GetKeyMembers(primaryEntity, tableKeySchema.PrimaryKeyMemberColumns, tableKeySchema.Name, out primaryMembersRequired);

            // 过滤没有外键主键的表处理
            if (foreignMembers == null || primaryMembers == null)
            {
                return(null);
            }
            Relationship foreignRelationship = ForeignEntity.Relationships
                                               .FirstOrDefault(r => r.RelationshipName == relationshipName && r.IsForeignKey);

            if (foreignRelationship == null)
            {
                foreignRelationship = new Relationship {
                    RelationshipName = relationshipName
                };
                ForeignEntity.Relationships.Add(foreignRelationship);
            }
            foreignRelationship.IsMapped        = true;
            foreignRelationship.IsForeignKey    = true;
            foreignRelationship.ThisCardinality = foreignMembersRequired ? Cardinality.One : Cardinality.ZeroOrOne;
            foreignRelationship.ThisEntity      = foreignName;
            foreignRelationship.ThisProperties  = new List <string>(foreignMembers);
            foreignRelationship.OtherEntity     = primaryName;
            foreignRelationship.OtherProperties = new List <string>(primaryMembers);
            foreignRelationship.CascadeDelete   = isCascadeDelete;

            string prefix = _content.GetMemberPrefix(foreignRelationship, primaryName, foreignName);

            string foreignPropertyName = _content.ToPropertyName(ForeignEntity.ClassName, prefix + primaryName);

            foreignPropertyName = _content.UniqueNamer.UniqueName(ForeignEntity.ClassName, foreignPropertyName);
            foreignRelationship.ThisPropertyName = foreignPropertyName;

            // add reverse
            Relationship primaryRelationship = primaryEntity.Relationships
                                               .FirstOrDefault(r => r.RelationshipName == relationshipName && r.IsForeignKey == false);

            if (primaryRelationship == null)
            {
                primaryRelationship = new Relationship {
                    RelationshipName = relationshipName
                };
                primaryEntity.Relationships.Add(primaryRelationship);
            }

            primaryRelationship.IsMapped        = false;
            primaryRelationship.IsForeignKey    = false;
            primaryRelationship.ThisEntity      = primaryName;
            primaryRelationship.ThisProperties  = new List <string>(primaryMembers);
            primaryRelationship.OtherEntity     = foreignName;
            primaryRelationship.OtherProperties = new List <string>(foreignMembers);
            primaryRelationship.CascadeDelete   = isCascadeDelete;

            bool isOneToOne = _content.IsOneToOne(tableKeySchema, foreignRelationship);

            if (isOneToOne)
            {
                primaryRelationship.ThisCardinality = primaryMembersRequired ? Cardinality.One : Cardinality.ZeroOrOne;
            }
            else
            {
                primaryRelationship.ThisCardinality = Cardinality.Many;
            }

            string primaryPropertyName = prefix + foreignName;

            if (!isOneToOne)
            {
                primaryPropertyName = _content.Settings.RelationshipName(primaryPropertyName);
            }

            primaryPropertyName = _content.ToPropertyName(primaryEntity.ClassName, primaryPropertyName);
            primaryPropertyName = _content.UniqueNamer.UniqueName(primaryEntity.ClassName, primaryPropertyName);

            primaryRelationship.ThisPropertyName = primaryPropertyName;

            foreignRelationship.OtherPropertyName = primaryRelationship.ThisPropertyName;
            foreignRelationship.OtherCardinality  = primaryRelationship.ThisCardinality;

            primaryRelationship.OtherPropertyName = foreignRelationship.ThisPropertyName;
            primaryRelationship.OtherCardinality  = foreignRelationship.ThisCardinality;

            foreignRelationship.IsProcessed = true;
            primaryRelationship.IsProcessed = true;

            return(primaryRelationship);
        }
Exemple #4
0
        public override TProperty GetAndCreate(DataObjectBase dataObjectBase)
        {
            if (Entity == null)
            {
                throw new ArgumentNullException("dataObjectBase");
            }

            TProperty property = Entity.Properties.ByColumn(dataObjectBase.Name);

            if (property != null)
            {
                return(property);
            }
            property = new TProperty {
                ColumnName = dataObjectBase.Name
            };

            string propertyName = Content.ToPropertyName(Entity.ClassName, dataObjectBase.Name);

            propertyName = Content.UniqueNamer.UniqueName(Entity.ClassName, propertyName);

            property.PropertyName = propertyName;

            property.DataType   = dataObjectBase.DataType;
            property.SystemType = dataObjectBase.SystemType;
            property.NativeType = dataObjectBase.NativeType;

            property.IsNullable = dataObjectBase.AllowDBNull;

            property.IsIdentity      = Content.IsIdentity(dataObjectBase);
            property.IsRowVersion    = Content.IsRowVersion(dataObjectBase);
            property.IsAutoGenerated = Content.IsDbGenerated(dataObjectBase);

            property.Default = Content.GetDefaultValue(dataObjectBase);

            property.Explain = string.IsNullOrWhiteSpace(dataObjectBase.Description) ? propertyName : dataObjectBase.Description;

            if (property.SystemType == typeof(string) ||
                property.SystemType == typeof(byte[]))
            {
                property.MaxLength = dataObjectBase.Size;
            }

            if (property.SystemType == typeof(float) ||
                property.SystemType == typeof(double) ||
                property.SystemType == typeof(decimal))
            {
                property.Precision = dataObjectBase.Precision;
                property.Scale     = dataObjectBase.Scale;
            }

            var columnSchema = dataObjectBase as ColumnSchema;

            if (columnSchema != null)
            {
                property.IsPrimaryKey = columnSchema.IsPrimaryKeyMember;
                property.IsForeignKey = columnSchema.IsForeignKeyMember;
                if (columnSchema.IsUnique)
                {
                    property.IsUnique = true;
                }
            }

            property.IsProcessed = true;
            return(property);
        }