Exemplo n.º 1
0
        /// <summary>
        /// 添加表前后缀
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="appDbContextAttribute">数据库上下文特性</param>
        /// <param name="entityTypeBuilder">实体类型构建器</param>
        /// <param name="dbContext">数据库上下文</param>
        /// <param name="dbContextType">数据库上下文类型</param>
        private static void AddTableAffixes(Type type, AppDbContextAttribute appDbContextAttribute, EntityTypeBuilder entityTypeBuilder, DbContext dbContext, Type dbContextType)
        {
            if (typeof(IPrivateEntityNotKey).IsAssignableFrom(type) || !string.IsNullOrEmpty(type.GetCustomAttribute <TableAttribute>(true)?.Schema))
            {
                return;
            }

            // 获取 多租户 Schema
            var dynamicSchema = !typeof(IMultiTenantOnSchema).IsAssignableFrom(dbContextType)
                ? default
                : dbContextType.GetMethod(nameof(IMultiTenantOnSchema.GetSchemaName)).Invoke(dbContext, null).ToString();

            if (appDbContextAttribute == null)
            {
                entityTypeBuilder.ToTable($"{type.Name}", dynamicSchema);
            }
            else
            {
                // 添加表统一前后缀,排除视图
                if (!string.IsNullOrEmpty(appDbContextAttribute.TableSuffix) || !string.IsNullOrEmpty(appDbContextAttribute.TableSuffix))
                {
                    var tablePrefix = appDbContextAttribute.TablePrefix;
                    var tableSuffix = appDbContextAttribute.TableSuffix;

                    if (!string.IsNullOrEmpty(tablePrefix))
                    {
                        // 如果前缀中找到 . 字符
                        if (tablePrefix.IndexOf(".") > 0)
                        {
                            var schema = tablePrefix.EndsWith(".") ? tablePrefix[0..^ 1] : tablePrefix;
Exemplo n.º 2
0
        /// <summary>
        /// 配置实体表名
        /// </summary>
        /// <param name="type">实体类型</param>
        /// <param name="appDbContextAttribute">数据库上下文特性</param>
        /// <param name="entityTypeBuilder">实体类型构建器</param>
        /// <param name="dbContext">数据库上下文</param>
        /// <param name="dbContextType">数据库上下文类型</param>
        private static void ConfigureEntityTableName(Type type, AppDbContextAttribute appDbContextAttribute, EntityTypeBuilder entityTypeBuilder, DbContext dbContext, Type dbContextType)
        {
            // 获取表是否定义 [Table] 特性
            var tableAttribute = type.IsDefined(typeof(TableAttribute), true) ? type.GetCustomAttribute <TableAttribute>(true) : default;

            // 排除无键实体或已经贴了 [Table] 特性的类型
            if (typeof(IPrivateEntityNotKey).IsAssignableFrom(type) || !string.IsNullOrWhiteSpace(tableAttribute?.Schema))
            {
                return;
            }

            // 获取真实表名
            var tableName = tableAttribute?.Name ?? type.Name;

            // 判断是否是启用了多租户模式,如果是,则获取 Schema
            var dynamicSchema = !typeof(IMultiTenantOnSchema).IsAssignableFrom(dbContextType)
                ? default
                : (dbContextType.GetMethod(nameof(IMultiTenantOnSchema.GetSchemaName))  // 支持显式和隐式
                   ?? typeof(IMultiTenantOnSchema).GetMethod(nameof(IMultiTenantOnSchema.GetSchemaName))
                   )?.Invoke(dbContext, null)?.ToString();

            // 获取类型前缀 [TablePrefix] 特性
            var tablePrefixAttribute = !type.IsDefined(typeof(TablePrefixAttribute), true)
                ? default
                : type.GetCustomAttribute <TablePrefixAttribute>(true);

            // 判断是否启用全局表前后缀支持或个别表自定义了前缀
            if (tablePrefixAttribute != null || appDbContextAttribute == null)
            {
                entityTypeBuilder.ToTable($"{tablePrefixAttribute?.Prefix}{tableName}", dynamicSchema);
            }
            else
            {
                // 添加表统一前后缀,排除视图
                if (!string.IsNullOrWhiteSpace(appDbContextAttribute.TablePrefix) || !string.IsNullOrWhiteSpace(appDbContextAttribute.TableSuffix))
                {
                    var tablePrefix = appDbContextAttribute.TablePrefix;
                    var tableSuffix = appDbContextAttribute.TableSuffix;

                    if (!string.IsNullOrWhiteSpace(tablePrefix))
                    {
                        // 如果前缀中找到 . 字符
                        if (tablePrefix.IndexOf(".") > 0)
                        {
                            var schema = tablePrefix.EndsWith(".") ? tablePrefix[0..^ 1] : tablePrefix;
Exemplo n.º 3
0
        /// <summary>
        /// 创建实体类型构建器
        /// </summary>
        /// <param name="type">数据库关联类型</param>
        /// <param name="modelBuilder">模型构建器</param>
        /// <param name="dbContext">数据库上下文</param>
        /// <param name="dbContextType">数据库上下文类型</param>
        /// <param name="dbContextLocator">数据库上下文定位器</param>
        /// <param name="dbContextCorrelationType"></param>
        /// <param name="appDbContextAttribute">数据库上下文特性</param>
        /// <returns>EntityTypeBuilder</returns>
        private static EntityTypeBuilder CreateEntityTypeBuilder(Type type, ModelBuilder modelBuilder, DbContext dbContext, Type dbContextType, Type dbContextLocator, DbContextCorrelationType dbContextCorrelationType, AppDbContextAttribute appDbContextAttribute = null)
        {
            // 反射创建实体类型构建器
            var entityTypeBuilder = ModelBuildEntityMethod.MakeGenericMethod(type).Invoke(modelBuilder, null) as EntityTypeBuilder;

            // 配置动态表名
            if (!ConfigureEntityMutableTableName(type, entityTypeBuilder, dbContext, dbContextLocator, dbContextCorrelationType))
            {
                // 配置实体表名
                ConfigureEntityTableName(type, appDbContextAttribute, entityTypeBuilder, dbContext, dbContextType);
            }

            // 如果未启用多租户支持或租户设置为OnDatabase 或 OnSchema 方案,则忽略多租户字段,另外还需要排除多租户数据库上下文定位器
            if (dbContextLocator != typeof(MultiTenantDbContextLocator) &&
                (!typeof(IPrivateMultiTenant).IsAssignableFrom(dbContextType) || typeof(IMultiTenantOnDatabase).IsAssignableFrom(dbContextType) || typeof(IMultiTenantOnSchema).IsAssignableFrom(dbContextType)) &&
                type.GetProperty(Db.OnTableTenantId) != null)
            {
                entityTypeBuilder.Ignore(Db.OnTableTenantId);
            }

            return(entityTypeBuilder);
        }