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 = 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()); } } } }