예제 #1
0
 public HasManyFluent HasForeignKey(string foreignKey)
 {
     if (foreignKey == null)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("foreignKey"));
     }
     foreach (string name in foreignKey.Split(','))
     {
         if (string.IsNullOrEmpty(name.Trim()))
         {
             continue;
         }
         _selfBind += ", " + name.Trim();
     }
     if (string.IsNullOrEmpty(_selfBind))
     {
         throw new ArgumentException(DbContextStrings.ParameterError("foreignKey"));
     }
     _selfBind = _selfBind.TrimStart(',', ' ');
     _tf.Navigate(_selfProperty, _selfBind);
     if (string.IsNullOrEmpty(_withOneProperty) == false)
     {
         _fsql.CodeFirst.ConfigEntity(_entityType2, eb2 => eb2.Navigate(_withOneProperty, _selfBind));
     }
     return(this);
 }
예제 #2
0
        public HasManyFluent <T2> HasMany <T2>(Expression <Func <T, IEnumerable <T2> > > many)
        {
            var exp = many?.Body;

            if (exp?.NodeType == ExpressionType.Convert)
            {
                exp = (exp as UnaryExpression)?.Operand;
            }
            if (exp == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("many"));
            }

            var manyProperty = "";

            switch (exp.NodeType)
            {
            case ExpressionType.MemberAccess:
                manyProperty = (exp as MemberExpression).Member.Name;
                break;
            }
            if (string.IsNullOrEmpty(manyProperty))
            {
                throw new ArgumentException(DbContextStrings.ParameterError("many"));
            }
            return(new HasManyFluent <T2>(_fsql, _tf, manyProperty));
        }
예제 #3
0
            public HasOneFluent <T2> WithOne(Expression <Func <T2, T> > one, Expression <Func <T2, object> > foreignKey)
            {
                var exp = one?.Body;

                if (exp?.NodeType == ExpressionType.Convert)
                {
                    exp = (exp as UnaryExpression)?.Operand;
                }
                if (exp == null)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("one"));
                }

                switch (exp.NodeType)
                {
                case ExpressionType.MemberAccess:
                    _withOneProperty = (exp as MemberExpression).Member.Name;
                    break;
                }
                if (string.IsNullOrEmpty(_withOneProperty))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError("one"));
                }

                exp = foreignKey?.Body;
                if (exp?.NodeType == ExpressionType.Convert)
                {
                    exp = (exp as UnaryExpression)?.Operand;
                }
                if (exp == null)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("foreignKey"));
                }

                switch (exp.NodeType)
                {
                case ExpressionType.MemberAccess:
                    _withOneBind = (exp as MemberExpression).Member.Name;
                    _withOneBind = _withOneBind.TrimStart(',', ' ');
                    break;

                case ExpressionType.New:
                    _withOneBind = "";
                    foreach (var member in (exp as NewExpression).Members)
                    {
                        _withOneBind += ", " + member.Name;
                    }
                    _withOneBind = _withOneBind.TrimStart(',', ' ');
                    break;
                }
                if (string.IsNullOrEmpty(_withOneBind))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError("foreignKey"));
                }
                if (string.IsNullOrEmpty(_selfBind) == false)
                {
                    _fsql.CodeFirst.ConfigEntity <T2>(eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
                }
                return(this);
            }
예제 #4
0
            public void WithMany(Expression <Func <T2, IEnumerable <T> > > many, Type middleType)
            {
                var exp = many?.Body;

                if (exp?.NodeType == ExpressionType.Convert)
                {
                    exp = (exp as UnaryExpression)?.Operand;
                }
                if (exp == null)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("many"));
                }

                switch (exp.NodeType)
                {
                case ExpressionType.MemberAccess:
                    _withManyProperty = (exp as MemberExpression).Member.Name;
                    break;
                }
                if (string.IsNullOrEmpty(_withManyProperty))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError("many"));
                }

                _tf.Navigate(_selfProperty, null, middleType);
                _fsql.CodeFirst.ConfigEntity <T2>(eb2 => eb2.Navigate(_withManyProperty, null, middleType));
            }
예제 #5
0
            public HasManyFluent <T2> WithOne(Expression <Func <T2, T> > one)
            {
                var exp = one?.Body;

                if (exp?.NodeType == ExpressionType.Convert)
                {
                    exp = (exp as UnaryExpression)?.Operand;
                }
                if (exp == null)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("one"));
                }

                switch (exp.NodeType)
                {
                case ExpressionType.MemberAccess:
                    _withOneProperty = (exp as MemberExpression).Member.Name;
                    break;
                }
                if (string.IsNullOrEmpty(_withOneProperty))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError("one"));
                }

                if (string.IsNullOrEmpty(_selfBind) == false)
                {
                    _fsql.CodeFirst.ConfigEntity <T2>(eb2 => eb2.Navigate(_withOneProperty, _selfBind));
                }
                return(this);
            }
예제 #6
0
        public EfCoreTableFluent <T> HasKey(Expression <Func <T, object> > key)
        {
            var exp = key?.Body;

            if (exp?.NodeType == ExpressionType.Convert)
            {
                exp = (exp as UnaryExpression)?.Operand;
            }
            if (exp == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("key"));
            }

            switch (exp.NodeType)
            {
            case ExpressionType.MemberAccess:
                _tf.Property((exp as MemberExpression).Member.Name).IsPrimary(true);
                break;

            case ExpressionType.New:
                foreach (var member in (exp as NewExpression).Members)
                {
                    _tf.Property(member.Name).IsPrimary(true);
                }
                break;
            }
            return(this);
        }
예제 #7
0
        static IServiceCollection AddFreeDbContext(this IServiceCollection services, Type dbContextType, Action <DbContextOptionsBuilder> options)
        {
            services.AddScoped(dbContextType, sp =>
            {
                DbContext ctx = null;
                try
                {
                    var ctor       = dbContextType.GetConstructors().FirstOrDefault();
                    var ctorParams = ctor.GetParameters().Select(a => sp.GetService(a.ParameterType)).ToArray();
                    ctx            = Activator.CreateInstance(dbContextType, ctorParams) as DbContext;
                }
                catch (Exception ex)
                {
                    throw new Exception(DbContextStrings.AddFreeDbContextError_CheckConstruction(dbContextType.Name), ex);
                }
                if (ctx != null && ctx._ormScoped == null)
                {
                    var builder = new DbContextOptionsBuilder();
                    options(builder);
                    ctx._ormScoped   = DbContextScopedFreeSql.Create(builder._fsql, () => ctx, () => ctx.UnitOfWork);
                    ctx._optionsPriv = builder._options;

                    if (ctx._ormScoped == null)
                    {
                        throw new Exception(DbContextStrings.ConfigureUseFreeSql);
                    }

                    ctx.InitPropSets();
                }
                return(ctx);
            });
            return(services);
        }
예제 #8
0
        public HasIndexFluent HasIndex(Expression <Func <T, object> > index)
        {
            var exp = index?.Body;

            if (exp?.NodeType == ExpressionType.Convert)
            {
                exp = (exp as UnaryExpression)?.Operand;
            }
            if (exp == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("index"));
            }

            var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
            var columns   = new List <string>();

            switch (exp.NodeType)
            {
            case ExpressionType.MemberAccess:
                columns.Add((exp as MemberExpression).Member.Name);
                break;

            case ExpressionType.New:
                foreach (var member in (exp as NewExpression).Members)
                {
                    columns.Add(member.Name);
                }
                break;
            }
            _tf.Index(indexName, string.Join(", ", columns), false);
            return(new HasIndexFluent(_tf, indexName, columns));
        }
예제 #9
0
 public HasOneFluent HasOne(string one)
 {
     if (string.IsNullOrEmpty(one))
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("one"));
     }
     if (_entityType.GetPropertiesDictIgnoreCase().TryGetValue(one, out var oneProperty) == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(one));
     }
     return(new HasOneFluent(_fsql, _tf, _entityType, oneProperty.PropertyType, one));
 }
예제 #10
0
 public EfCoreTableFluent HasKey(string key)
 {
     if (key == null)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("key"));
     }
     foreach (string name in key.Split(','))
     {
         if (string.IsNullOrEmpty(name.Trim()))
         {
             continue;
         }
         _tf.Property(name.Trim()).IsPrimary(true);
     }
     return(this);
 }
예제 #11
0
 public HasManyFluent HasMany(string many)
 {
     if (string.IsNullOrEmpty(many))
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("many"));
     }
     if (_entityType.GetPropertiesDictIgnoreCase().TryGetValue(many, out var manyProperty) == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_CollectionProperties(many));
     }
     if (typeof(IEnumerable).IsAssignableFrom(manyProperty.PropertyType) == false || manyProperty.PropertyType.IsGenericType == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_IsNot_CollectionProperties(many));
     }
     return(new HasManyFluent(_fsql, _tf, _entityType, manyProperty.PropertyType.GetGenericArguments()[0], manyProperty.Name));
 }
예제 #12
0
 public HasOneFluent WithMany(string many)
 {
     if (many == null)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("many"));
     }
     if (_entityType2.GetPropertiesDictIgnoreCase().TryGetValue(many, out var manyProperty) == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(many));
     }
     _withManyProperty = manyProperty.Name;
     if (string.IsNullOrEmpty(_selfBind) == false)
     {
         _fsql.CodeFirst.ConfigEntity(_entityType2, eb2 => eb2.Navigate(many, _selfBind));
     }
     return(this);
 }
예제 #13
0
 public void WithMany(string many, Type middleType)
 {
     if (string.IsNullOrEmpty(many))
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("many"));
     }
     if (_entityType2.GetPropertiesDictIgnoreCase().TryGetValue(many, out var manyProperty) == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_CollectionProperties(many));
     }
     if (typeof(IEnumerable).IsAssignableFrom(manyProperty.PropertyType) == false || manyProperty.PropertyType.IsGenericType == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_IsNot_CollectionProperties(many));
     }
     _withManyProperty = manyProperty.Name;
     _tf.Navigate(_selfProperty, null, middleType);
     _fsql.CodeFirst.ConfigEntity(_entityType2, eb2 => eb2.Navigate(_withManyProperty, null, middleType));
 }
예제 #14
0
        public HasIndexFluent HasIndex(string index)
        {
            if (index == null)
            {
                throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("index"));
            }
            var indexName = $"idx_{Guid.NewGuid().ToString("N").Substring(0, 8)}";
            var columns   = new List <string>();

            foreach (string name in index.Split(','))
            {
                if (string.IsNullOrEmpty(name.Trim()))
                {
                    continue;
                }
                columns.Add(name.Trim());
            }
            _tf.Index(indexName, string.Join(", ", columns), false);
            return(new HasIndexFluent(_tf, indexName, columns));
        }
예제 #15
0
 public HasManyFluent WithOne(string one)
 {
     if (string.IsNullOrEmpty(one))
     {
         throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("one"));
     }
     if (_entityType2.GetPropertiesDictIgnoreCase().TryGetValue(one, out var oneProperty) == false)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(one));
     }
     if (oneProperty.PropertyType != _entityType1)
     {
         throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(one));
     }
     _withOneProperty = oneProperty.Name;
     if (string.IsNullOrEmpty(_selfBind) == false)
     {
         _fsql.CodeFirst.ConfigEntity(_entityType2, eb2 => eb2.Navigate(oneProperty.Name, _selfBind));
     }
     return(this);
 }
예제 #16
0
            public HasOneFluent WithOne(string one, string foreignKey)
            {
                if (string.IsNullOrEmpty(one))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("one"));
                }
                if (_entityType1.GetPropertiesDictIgnoreCase().TryGetValue(one, out var oneProperty) == false)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(one));
                }
                if (oneProperty != _entityType1)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_NotFound_Property(one));
                }
                _withOneProperty = oneProperty.Name;

                if (foreignKey == null)
                {
                    throw new ArgumentException(DbContextStrings.ParameterError_CannotBeNull("foreignKey"));
                }
                foreach (string name in foreignKey.Split(','))
                {
                    if (string.IsNullOrEmpty(name.Trim()))
                    {
                        continue;
                    }
                    _withOneBind += ", " + name.Trim();
                }
                if (string.IsNullOrEmpty(_withOneBind))
                {
                    throw new ArgumentException(DbContextStrings.ParameterError("foreignKey"));
                }
                _withOneBind = _withOneBind.TrimStart(',', ' ');
                if (string.IsNullOrEmpty(_selfBind) == false)
                {
                    _fsql.CodeFirst.ConfigEntity(_entityType2, eb2 => eb2.Navigate(_withOneProperty, _withOneBind));
                }
                return(this);
            }
예제 #17
0
        TEntity CheckTKeyAndReturnIdEntity(TKey id)
        {
            var tb = _db.OrmOriginal.CodeFirst.GetTableByEntity(EntityType);

            if (tb.Primarys.Length != 1)
            {
                throw new Exception(DbContextStrings.EntityType_PrimaryKeyIsNotOne(EntityType.Name));
            }
            if (tb.Primarys[0].CsType.NullableTypeOrThis() != typeof(TKey).NullableTypeOrThis())
            {
                throw new Exception(DbContextStrings.EntityType_PrimaryKeyError(EntityType.Name, typeof(TKey).FullName));
            }
            var obj = Activator.CreateInstance(tb.Type);

            _db.OrmOriginal.SetEntityValueWithPropertyName(tb.Type, obj, tb.Primarys[0].CsName, id);
            var ret = obj as TEntity;

            if (ret == null)
            {
                throw new Exception(DbContextStrings.EntityType_CannotConvert(EntityType.Name, typeof(TEntity).Name));
            }
            return(ret);
        }
예제 #18
0
        public void SpecialError_UpdateFailedDataNotTrackedTest()
        {
            string text = DbContextStrings.SpecialError_UpdateFailedDataNotTracked("1");

            output.WriteLine(text);
        }
예제 #19
0
        public void TypeHasSetProperty_IgnoreAttributeTest()
        {
            string text = DbContextStrings.TypeHasSetProperty_IgnoreAttribute("1", "2");

            output.WriteLine(text);
        }
예제 #20
0
        public void AddFreeDbContextError_CheckConstructionTest()
        {
            string text = DbContextStrings.AddFreeDbContextError_CheckConstruction("1");

            output.WriteLine(text);
        }
예제 #21
0
        public void UnitOfWorkManager_Construction_CannotBeNullTest()
        {
            string text = DbContextStrings.UnitOfWorkManager_Construction_CannotBeNull("1", "2");

            output.WriteLine(text);
        }
예제 #22
0
        public void CannotDelete_DataNotTracked_ShouldQueryTest()
        {
            string text = DbContextStrings.CannotDelete_DataNotTracked_ShouldQuery("1");

            output.WriteLine(text);
        }
예제 #23
0
        public void UpdateError_FilterTest()
        {
            string text = DbContextStrings.UpdateError_Filter("1", "2", "3");

            output.WriteLine(text);
        }
예제 #24
0
        public void CannotAttach_PrimaryKey_NotSetTest()
        {
            string text = DbContextStrings.CannotAttach_PrimaryKey_NotSet("1");

            output.WriteLine(text);
        }
예제 #25
0
        public void SpecialError_BatchAdditionFailedTest()
        {
            string text = DbContextStrings.SpecialError_BatchAdditionFailed("1");

            output.WriteLine(text);
        }
예제 #26
0
        public void CannotDelete_EntityHasNo_PrimaryKeyTest()
        {
            string text = DbContextStrings.CannotDelete_EntityHasNo_PrimaryKey("1");

            output.WriteLine(text);
        }
예제 #27
0
        public void PropertyOfType_IsNot_OneToManyOrManyToManyTest()
        {
            string text = DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany("1", "2");

            output.WriteLine(text);
        }
예제 #28
0
        public void CannotAdd_AlreadyExistsInStateManagementTest()
        {
            string text = DbContextStrings.CannotAdd_AlreadyExistsInStateManagement("1");

            output.WriteLine(text);
        }
예제 #29
0
        public void ParameterError_NotFound_PropertyTest()
        {
            string text = DbContextStrings.ParameterError_NotFound_Property("1");

            output.WriteLine(text);
        }
예제 #30
0
        public void CannotAdd_SelfIncreasingHasValueTest()
        {
            string text = DbContextStrings.CannotAdd_SelfIncreasingHasValue("1");

            output.WriteLine(text);
        }