一个Dotnet数据框架的bug
好久没写C#代码了,今天在维护公司老项目时,偶然发现一个BUG。记录一下,后面的同学就不要踩坑啦。
-------------------------------------------------------------------
该项目采用了一个国产的数据访问框架 PDF.NET ,但是我有一个表的主键是bigint,数据编号已经大于Int32.MaxValue,此时就不能再插入数据了。原因如下(红色部分转换报错):
internal static int InsertInner(EntityBase entity, List<string> objFields, CommonDB DB)
{
if (objFields == null || objFields.Count == 0)
return 0;
IDataParameter[] paras = new IDataParameter[objFields.Count];
string tableName = entity.TableName;
string identityName = entity.IdentityName;
string sql = "INSERT INTO " + entity.GetSchemeTableName();
string fields = "";
string values = "";
int index = 0;
//获取实体属性信息缓存
var entityFieldsCache = EntityFieldsCache.Item(entity.GetType());
foreach (string field in objFields)
{
if (identityName != field)
{
fields += ",[" + field + "]";
string paraName = DB.GetParameterChar + "P" + index.ToString();
values += "," + paraName;
paras[index] = DB.GetParameter(paraName, entity.PropertyList(field));
//从缓存中获取当前field所对应的类型
Type fieldType = entityFieldsCache.GetPropertyType(field);
if (fieldType == typeof(string) && paras[index].Value != null)
//为字符串类型的参数指定长度 edit at 2012.4.23
//((IDbDataParameter)paras[index]).Size = entity.GetStringFieldSize(field);
SetParameterSize(ref paras[index], entity, field,DB);
else if (fieldType == typeof(byte[]))
//为字节类型指定转换类型,防止空值时被当作字符串类型
paras[index].DbType = DbType.Binary;
index++;
}
}
sql = sql + "(" + fields.TrimStart(',') + ") VALUES (" + values.TrimStart(',') + ")";
int count = 0;
if (identityName != "")
{
//有自增字段
object id = entity.PropertyList(identityName);
EntityCommand ec = new EntityCommand(entity, DB);
string insertKey = ec.GetInsertKey();
count = DB.ExecuteInsertQuery(sql, CommandType.Text, paras, ref id,insertKey);
entity.setProperty(identityName, Convert.ToInt32(id));
}
else
{
count = DB.ExecuteNonQuery(sql, CommandType.Text, paras);
}
if (count > 0)
entity.ResetChanges();
return count;
}
该bug已经给作者说了,期待他们的及时修复。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?