Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsTable方法不起作用 #1916

Open
manasheep opened this issue Nov 7, 2024 · 1 comment
Open

AsTable方法不起作用 #1916

manasheep opened this issue Nov 7, 2024 · 1 comment

Comments

@manasheep
Copy link

问题描述及重现代码:

基本需求是,读取数据表A里的数据,筛掉部分数据之后之后,将有效数据写入数据表A_fix,想通过 AsTable 来指定写入数据表的表名。
我写了以下方法来实现,但是发现执行的SQL代码中,并不是newtablename表名,而是原来的默认表名(TableAttribute指定的表名)。
PS:我的写入数据库是达梦的

// c# code
        /// <summary>
        /// 替换表格数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="dataList">数据列表</param>
        /// <param name="db">数据库</param>
        /// <param name="tableNameSuffix">表名尾缀,基于原表名增加的尾缀</param>
        /// <exception cref="NotImplementedException"></exception>
        private void replaceTableData<T>(List<T> dataList, IFreeSql db, string tableNameSuffix) where T : class
        {
            // 获取类型
            var type = typeof(T);
            // 获取 Table 特性
            var tableAttribute = type.GetCustomAttribute<FreeSql.DataAnnotations.TableAttribute>();
            // 新表名
            var newtablename = tableAttribute.Name + tableNameSuffix;

            try
            {
                var delResult = db.Delete<T>().AsTable(newtablename).Where(q => true).ExecuteAffrows();
            }
            catch (Exception er)
            {
                Log4NetExtension.GetLogger().Error($"[{type.FullName}]删除表数据时发生异常", er);
            }
            var insResult = db.Insert<T>(dataList).AsTable(newtablename).ExecuteAffrows();
        }

数据库版本

安装的Nuget包

.net framework/. net core? 及具体版本

@2881099
Copy link
Collaborator

2881099 commented Nov 7, 2024

统一加前缀,建议看aop文档。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants