public ShardingQueryable(IQueryable <T> source, ShardingDbAccessor repository, string absDbName) { _source = source; _absTableName = AnnotationHelper.GetDbTableName(source.ElementType); _absDbName = absDbName; _repository = repository; }
public ShardingQueryable(IQueryable <T> source, ShardingDbAccessor shardingDb, IShardingConfig shardingConfig, IDbFactory dbFactory) { _source = source; _absTableName = AnnotationHelper.GetDbTableName(source.ElementType); _shardingConfig = shardingConfig; _shardingDb = shardingDb; _dbFactory = dbFactory; }
private (string sql, List <(string paramterName, object paramterValue)> paramters) GetDeleteSql(IQueryable iq) { string tableName = AnnotationHelper.GetDbTableName(iq.ElementType); var whereSql = GetWhereSql(iq); string sql = $"DELETE FROM {FormatFieldName(tableName)} WHERE {whereSql.sql}"; return(sql, whereSql.paramters); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { List <Type> entityTypes; if (Options.EntityTypes?.Length > 0) { entityTypes = Options.EntityTypes.ToList(); } else { var q = Constant.AllTypes.Where(x => x.GetCustomAttribute(typeof(TableAttribute), false) != null); //通过Namespace解决同表名问题 if (!Options.EntityNamespace.IsNullOrEmpty()) { q = q.Where(x => x.Namespace.Contains(Options.EntityNamespace)); } entityTypes = q.ToList(); } entityTypes.ForEach(aEntity => { var entity = modelBuilder.Entity(aEntity); if (!string.IsNullOrEmpty(Options.Suffix)) { entity.ToTable($"{AnnotationHelper.GetDbTableName(aEntity)}_{Options.Suffix}"); } }); //支持IEntityTypeConfiguration配置 var entityTypeConfigurationTypes = Constant.AllTypes .Where(x => x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>) && entityTypes.Contains(y.GetGenericArguments()[0]) )) .ToList(); entityTypeConfigurationTypes.ForEach(aConfig => { modelBuilder.ApplyConfiguration((dynamic)Activator.CreateInstance(aConfig)); }); //DateTime默认为Local foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) { property.SetValueConverter(_dateTimeConverter); } } } }
protected override void OnModelCreating(ModelBuilder modelBuilder) { List <Type> entityTypes; if (Options.EntityTypes?.Length > 0) { entityTypes = Options.EntityTypes.ToList(); } else { var q = Constant.AllEntityTypes.Where(x => x.GetCustomAttribute(typeof(TableAttribute), false) != null); //通过Namespace解决同表名问题 if (!Options.EntityNamespace.IsNullOrEmpty()) { q = q.Where(x => x.Namespace.Contains(Options.EntityNamespace)); } entityTypes = q.ToList(); } //支持IEntityTypeConfiguration配置 Constant.Assemblies.Distinct().ToList().ForEach(aAssembly => { modelBuilder.ApplyConfigurationsFromAssembly(aAssembly, x => { //仅加载对应实体配置 return(entityTypes.Any(y => typeof(IEntityTypeConfiguration <>).MakeGenericType(y).IsAssignableFrom(x))); }); }); entityTypes.ForEach(aEntity => { var entity = modelBuilder.Entity(aEntity); if (!string.IsNullOrEmpty(Options.Suffix)) { entity.ToTable($"{AnnotationHelper.GetDbTableName(aEntity)}_{Options.Suffix}"); } }); //DateTime默认为Local foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) { property.SetValueConverter(_dateTimeConverter); } } } }
private string GetFormatedSchemaAndTableName(Type entityType) { string schema = AnnotationHelper.GetDbSchemaName(entityType); schema = GetSchema(schema); string table = AnnotationHelper.GetDbTableName(entityType); if (schema.IsNullOrEmpty()) { return(FormatFieldName(table)); } else { return($"{FormatFieldName(schema)}.{FormatFieldName(table)}"); } }
private string GetFormatedSchemaAndTableName(Type entityType) { string fullName = string.Empty; string schema = AnnotationHelper.GetDbSchemaName(entityType); schema = GetSchema(schema); string table = AnnotationHelper.GetDbTableName(entityType); if (!_db.Paramter.Suffix.IsNullOrEmpty()) { table += $"_{_db.Paramter.Suffix}"; } if (schema.IsNullOrEmpty()) { fullName = FormatFieldName(table); } else { fullName = $"{FormatFieldName(schema)}.{FormatFieldName(table)}"; } return(fullName); }
/// <summary> /// 模型构建 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { List <Type> entityTypes; if (Paramter.EntityTypes?.Length > 0) { entityTypes = Paramter.EntityTypes.ToList(); } else { var q = ShardingOption.Types.Where(x => x.GetCustomAttribute(typeof(TableAttribute), false) != null); //通过Namespace解决同表名问题 if (!Paramter.EntityNamespace.IsNullOrEmpty()) { q = q.Where(x => x.Namespace.Contains(Paramter.EntityNamespace)); } entityTypes = q.ToList(); } entityTypes.ForEach(aEntity => { var entity = modelBuilder.Entity(aEntity); ShardingOption.EntityTypeBuilderFilter?.Invoke(entity); if (!string.IsNullOrEmpty(Paramter.Suffix)) { entity.ToTable($"{AnnotationHelper.GetDbTableName(aEntity)}_{Paramter.Suffix}"); } }); //支持IEntityTypeConfiguration配置 var entityTypeConfigurationTypes = ShardingOption.Types .Where(x => x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>) && entityTypes.Contains(y.GetGenericArguments()[0]) )) .ToList(); entityTypeConfigurationTypes.ForEach(aConfig => { modelBuilder.ApplyConfiguration((dynamic)Activator.CreateInstance(aConfig)); }); //DateTime默认为Local foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) { property.SetValueConverter(_dateTimeConverter); } } } #if EFCORE3 //字段注释,需要开启程序集XML文档 foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { var comments = XmlHelper.GetProperyCommentBySummary(entityType.ClrType); foreach (var property in entityType.GetProperties()) { if (comments.ContainsKey(property.Name)) { property.SetComment(comments[property.Name]); } } } #endif }
/// <summary> /// 模型构建 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(ModelBuilder modelBuilder) { List <Type> entityTypes; if (Paramter.EntityTypes?.Length > 0) { entityTypes = Paramter.EntityTypes.ToList(); } else { var q = EFCoreShardingOptions.Types.Where(x => x.GetCustomAttribute(typeof(TableAttribute), false) != null); //通过Namespace解决同表名问题 if (!Paramter.EntityNamespace.IsNullOrEmpty()) { q = q.Where(x => x.Namespace.Contains(Paramter.EntityNamespace)); } entityTypes = q.ToList(); } entityTypes.ForEach(aEntity => { var entity = modelBuilder.Entity(aEntity); ShardingOption.EntityTypeBuilderFilter?.Invoke(entity); if (!string.IsNullOrEmpty(Paramter.Suffix)) { entity.ToTable($"{AnnotationHelper.GetDbTableName(aEntity)}_{Paramter.Suffix}", AnnotationHelper.GetDbSchemaName(aEntity)); } }); //支持IEntityTypeConfiguration配置 entityTypes.ForEach(aEntityType => { var entityTypeConfigurationTypes = EFCoreShardingOptions.Types .Where(x => x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>) && aEntityType == y.GetGenericArguments()[0]) ) .ToList(); entityTypeConfigurationTypes.ForEach(aEntityConfig => { var method = modelBuilder.GetType().GetMethods() .Where(x => x.Name == nameof(ModelBuilder.ApplyConfiguration) && x.GetParameters().Count() == 1 && x.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IEntityTypeConfiguration <>) ) .FirstOrDefault(); method.MakeGenericMethod(aEntityType).Invoke(modelBuilder, new object[] { Activator.CreateInstance(aEntityConfig) }); }); }); //DateTime默认为Local foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) { property.SetValueConverter(_dateTimeConverter); } } } //字段注释,需要开启程序集XML文档 if (ShardingOption.EnableComments) { foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { foreach (var property in entityType.GetProperties()) { if (property.PropertyInfo == null) { continue; } StringBuilder comment = new StringBuilder(property.PropertyInfo.GetXmlDocsSummary()); if (property.PropertyInfo.PropertyType.IsEnum) { foreach (var aValue in Enum.GetValues(property.PropertyInfo.PropertyType)) { var memberComment = property.PropertyInfo.PropertyType.GetMembers() .Where(x => x.Name == aValue.ToString()) .FirstOrDefault()? .GetXmlDocsSummary(); comment.Append($" {(int)aValue}={memberComment}"); } } property.SetComment(comment.ToString()); } } } }