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); }
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)); }
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); }
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)); }
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); }
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); }
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); }
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)); }
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)); }
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); }
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)); }
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); }
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)); }
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)); }
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); }
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); }
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); }
public void SpecialError_UpdateFailedDataNotTrackedTest() { string text = DbContextStrings.SpecialError_UpdateFailedDataNotTracked("1"); output.WriteLine(text); }
public void TypeHasSetProperty_IgnoreAttributeTest() { string text = DbContextStrings.TypeHasSetProperty_IgnoreAttribute("1", "2"); output.WriteLine(text); }
public void AddFreeDbContextError_CheckConstructionTest() { string text = DbContextStrings.AddFreeDbContextError_CheckConstruction("1"); output.WriteLine(text); }
public void UnitOfWorkManager_Construction_CannotBeNullTest() { string text = DbContextStrings.UnitOfWorkManager_Construction_CannotBeNull("1", "2"); output.WriteLine(text); }
public void CannotDelete_DataNotTracked_ShouldQueryTest() { string text = DbContextStrings.CannotDelete_DataNotTracked_ShouldQuery("1"); output.WriteLine(text); }
public void UpdateError_FilterTest() { string text = DbContextStrings.UpdateError_Filter("1", "2", "3"); output.WriteLine(text); }
public void CannotAttach_PrimaryKey_NotSetTest() { string text = DbContextStrings.CannotAttach_PrimaryKey_NotSet("1"); output.WriteLine(text); }
public void SpecialError_BatchAdditionFailedTest() { string text = DbContextStrings.SpecialError_BatchAdditionFailed("1"); output.WriteLine(text); }
public void CannotDelete_EntityHasNo_PrimaryKeyTest() { string text = DbContextStrings.CannotDelete_EntityHasNo_PrimaryKey("1"); output.WriteLine(text); }
public void PropertyOfType_IsNot_OneToManyOrManyToManyTest() { string text = DbContextStrings.PropertyOfType_IsNot_OneToManyOrManyToMany("1", "2"); output.WriteLine(text); }
public void CannotAdd_AlreadyExistsInStateManagementTest() { string text = DbContextStrings.CannotAdd_AlreadyExistsInStateManagement("1"); output.WriteLine(text); }
public void ParameterError_NotFound_PropertyTest() { string text = DbContextStrings.ParameterError_NotFound_Property("1"); output.WriteLine(text); }
public void CannotAdd_SelfIncreasingHasValueTest() { string text = DbContextStrings.CannotAdd_SelfIncreasingHasValue("1"); output.WriteLine(text); }