public InsqlEntityPropertyBuilder(IInsqlEntityMap entityMap, PropertyInfo propertyInfo)
        {
            if (entityMap.Properties.Any(o => o.PropertyInfo.Name == propertyInfo.Name))
            {
                throw new Exception($"insql entity builder type : {entityMap.EntityType} `{propertyInfo.Name}` property have been mapped!");
            }

            this.entityMap = entityMap;

            this.propertyMap = new InsqlPropertyMap(propertyInfo);

            this.entityMap.Properties.Add(this.propertyMap);
        }
Exemplo n.º 2
0
        private IInsqlEntityMap CreateAnnotationEntityMap(Type entityType)
        {
            TableAttribute tableAttribute = (TableAttribute)entityType.GetCustomAttribute(typeof(TableAttribute), true);

            IInsqlEntityMap resultMap = new InsqlEntityMap(entityType, tableAttribute.Name, tableAttribute.Schema);

            var columnMaps = entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(propInfo =>
            {
                ColumnAttribute columnAttribute       = (ColumnAttribute)propInfo.GetCustomAttribute(typeof(ColumnAttribute), true);
                KeyAttribute keyAttribute             = (KeyAttribute)propInfo.GetCustomAttribute(typeof(KeyAttribute), true);
                NotMappedAttribute notMappedAttribute = (NotMappedAttribute)propInfo.GetCustomAttribute(typeof(NotMappedAttribute), true);
                EditableAttribute editableAttribute   = (EditableAttribute)propInfo.GetCustomAttribute(typeof(EditableAttribute), true);
                DatabaseGeneratedAttribute databaseGeneratedAttribute = (DatabaseGeneratedAttribute)propInfo.GetCustomAttribute(typeof(DatabaseGeneratedAttribute), true);

                InsqlPropertyMap propertyMap = new InsqlPropertyMap(propInfo, columnAttribute?.Name);

                if (keyAttribute != null)
                {
                    propertyMap.IsKey = true;
                }
                if (notMappedAttribute != null)
                {
                    propertyMap.IsIgnored = true;
                }
                if (databaseGeneratedAttribute != null)
                {
                    if (databaseGeneratedAttribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
                    {
                        propertyMap.IsIdentity = true;
                    }
                }
                if (editableAttribute != null && !editableAttribute.AllowEdit)
                {
                    propertyMap.IsReadonly = true;
                }

                return(propertyMap);
            });

            foreach (var columnMap in columnMaps)
            {
                resultMap.Properties.Add(columnMap);
            }

            InsqlEntityValidator.Instance.Validate(resultMap);

            return(resultMap);
        }