Exemplo n.º 1
0
        private IEntityRelationship GetPreviousRelationship()
        {
            var entityType  = typeof(TEntity);
            var relatedType = typeof(TRelatedEntity);

            var entityPreviousRelationship        = storage_.Get(entityType)?.Relationships.FirstOrDefault(r => r.Entity == entityType && r.RelatedEntity == relatedType);
            var relatedEntityPreviousRelationship = storage_.Get(relatedType)?.Relationships.FirstOrDefault(r => r.Entity == relatedType && r.RelatedEntity == entityType);

            return(entityPreviousRelationship ?? relatedEntityPreviousRelationship);
        }
Exemplo n.º 2
0
        public IEntityBuilder <TEntity> HasKey <TKey>(Expression <Func <TEntity, TKey> > propertySelector)
        {
            var propertyInfo = propertySelector.GetPropertyInfo();

            var entityType = GetEntityType <TEntity>();

            var modelData = storage_.Get(entityType);

            var key = new PrimaryKey(propertyInfo.PropertyType, propertyInfo);

            modelData.Keys.Add(key);

            return(new EntityBuilder <TEntity>(storage_));
        }
Exemplo n.º 3
0
        public IEntityDataBuilder <TEntity> HasForeignKey <TKey>(Expression <Func <TEntity, TKey> > dataSelector)
        {
            var propertyInfo = dataSelector.GetPropertyInfo();

            var entityType = typeof(TEntity);

            storage_.Get(entityType)?.Keys.Add(new ForeignKey(propertyInfo.PropertyType, propertyInfo, relationship_));

            return(this);
        }
        public IEntityPropertyOptionsBuilder <TEntity> Ignore()
        {
            var property = storage_?.Get(typeof(TEntity))?.EntityPropertiesData.FirstOrDefault(p => p.Property.PropertyType == propertyInfo_.PropertyType);

            if (property != null)
            {
                property.IsIgnored = true;
            }

            return(this);
        }
Exemplo n.º 5
0
        private bool IsPropertyIgnored(Type entityType, PropertyInfo property)
        {
            var isIgnored = modelDataStorage_.Get(entityType)?.EntityPropertiesData.Any(p => p.Property == property && p.IsIgnored);

            if (isIgnored == null)
            {
                return(false);
            }

            return(isIgnored.Value);
        }
Exemplo n.º 6
0
        public IEntityPropertyOptionsBuilder <TEntity> HasProperty <TProperty>(Expression <Func <TEntity, TProperty> > propertySelector)
        {
            var key          = typeof(TEntity);
            var propertyInfo = propertySelector.GetPropertyInfo();

            storage_.Get(typeof(TEntity))?.EntityPropertiesData.Add(new EntityPropertyData()
            {
                Property = propertyInfo
            });

            return(new EntityPropertyOptionsBuilder <TEntity>(storage_, propertyInfo));
        }
Exemplo n.º 7
0
        public void CreateDatabase()
        {
            OnModelCreating(new ModelBuilder(modelDataStorage_));

            if (!CheckDatabaseExists(options_.ConnectionString, options_.DatabaseName))
            {
                var result = database_.ExecuteCommand(database_.CreateCommand(b => b.WithConnectionString(options_.ConnectionString).WithCommandText($"CREATE DATABASE {options_.DatabaseName}")));
            }

            var entities = this.GetType()
                           .GetProperties()
                           .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DatabaseTable <>))
                           .Select(p => new EntityData
            {
                PropertyName = p.Name,
                EntityType   = p.PropertyType.GetGenericArguments()[0]
            });

            dbTableToPropertyName_ = entities.ToDictionary(k => k.EntityType, k => k.PropertyName);

            entities.Each(entity =>
            {
                ProcessDatabaseTableCreation(entity);
            })
            .Each(entity =>
            {
                modelDataStorage_.Get(entity.EntityType).Keys
                .Each(key =>
                {
                    ProcessConstraints(entity, key);
                });
            });

            this.GetType()
            .GetProperties()
            .Where(p => p.PropertyType.GetGenericTypeDefinition() == typeof(DatabaseTable <>))
            .Select(p => new
            {
                Property        = p,
                GenericArgument = p.PropertyType.GetGenericArguments()[0]
            })
            .Each(e =>
            {
                e.Property.SetValue(this, Activator.CreateInstance(typeof(InternalDatabaseTable <>).MakeGenericType(e.GenericArgument), new object[5] {
                    database_, stateManager_, new GenericExpressionVisitor(), e.Property.Name, null
                }));
                tableNameProvider_.AddTableName(e.GenericArgument, e.Property.Name);
            });
        }