gpt4 book ai didi

c# - EF Core - 字符串或二进制数据将被截断

转载 作者:行者123 更新时间:2023-12-04 11:27:28 29 4
gpt4 key购买 nike

当您有 80(+/-) 列可供选择时,您如何确定哪一列是罪魁祸首?使用 .Net Core (netcoreapp2.2) 和 EF Core 2.2.4。

拿起一些现有的代码,并尝试跟踪失败的列。但是,它不起作用。我在这里和其他地方查看了数十个示例,但没有找到在 EF Core 2.x 中执行此操作的方法。

public int GetColumnMaxLength(string table, EntityEntry entityEntry)
{
// Just a rough to get the right data - always returns 0 for the moment...
int result = 0;
var modelContext = entityEntry.Context;
var entityType = modelContext.Model.FindEntityType(table); // THIS IS ALWAYS NULL!

if (entityType != null)
{
// Table info
var tableName = entityType.Relational().TableName;
var tableSchema = entityType.Relational().Schema;

// Column info
foreach (var property in entityType.GetProperties())
{
var columnName = property.Relational().ColumnName;
var columnType = property.Relational().ColumnType;
var isFixedLength = property.Relational().IsFixedLength;
};
}
return result;
}

上面的代码由围绕 db.SaveAsync() 的 try/catch 的这个 catch 部分调用;陈述。
catch (Exception ex)
{
// -----------------------------------------
// no idea what this was really trying to
// do as it barfs out all columns...
// -----------------------------------------

var dataInfo = new DataInfo();

var strLargeValues = new List<Tuple<int, string, string, string>>();

foreach (var entityEntry in _db.ChangeTracker.Entries().Where(et => et.State != EntityState.Unchanged))
{
// -----------------------------------------
// try to get the column info for all
// columns on this table...
// -----------------------------------------
dataInfo.GetColumnMaxLength("Subscription", entityEntry);

foreach (var entry in entityEntry.CurrentValues.Properties)
{
var value = entry.PropertyInfo.GetValue(entityEntry.Entity);
if (value is string s)
{
strLargeValues.Add(Tuple.Create(s.Length, s, entry.Name, entityEntry.Entity.GetType().Name));
}
}
}

var l = strLargeValues.OrderByDescending(v => v.Item1).ToArray();

foreach (var x in l.Take(100))
{
Trace.WriteLine(x.Item4 + " - " + x.Item3 + " - " + x.Item1 + ": " + x.Item2);
}

throw;
}

所以,问题的关键是:如何从 EF Core 获取 SQL 列定义?

我希望能够在 incomingData.Length > targetColumnDefinition.Length 时记录特定的表和列

最终解决方案:
public override int SaveChanges()
{
using (LogContext.PushProperty("DbContext:Override:Save", nameof(SaveChanges)))
{
try
{
return base.SaveChanges();
}
catch (Exception ex)
{
var errorMessage = String.Empty;
var token = Environment.NewLine;

foreach (var entityEntry in this.ChangeTracker.Entries().Where(et => et.State != EntityState.Unchanged))
{
foreach (var entry in entityEntry.CurrentValues.Properties)
{
var result = entityEntry.GetDatabaseDefinition(entry.Name);
var value = entry.PropertyInfo.GetValue(entityEntry.Entity);
if (result.IsFixedLength && value.ToLength() > result.MaxLength)
{
errorMessage = $"{errorMessage}{token}ERROR!! <<< {result.TableName}.{result.ColumnName} {result.ColumnType.ToUpper()} :: {entry.Name}({value.ToLength()}) = {value} >>>";
Log.Warning("Cannot save data to SQL column {TableName}.{ColumnName}! Max length is {LengthTarget} and you are trying to save something that is {LengthSource}. Column definition is {ColumnType}"
, result.TableName
, result.ColumnName
, result.MaxLength
, value.ToLength()
, result.ColumnType);
}
}
}
throw new Exception(errorMessage, ex);
}
}
}

最佳答案

正如评论中提到的,您需要全名,这可以从元数据中读取。

public int GetColumnMaxLength(EntityEntry entityEntry)
{
int result = 0;

var table = entityEntry.Metadata.Model.FindEntityType(entityEntry.Metadata.ClrType);

// Column info
foreach (var property in table.GetProperties())
{
var maxLength = property.GetMaxLength();

// For sql info, e.g. ColumnType = nvarchar(255):
var sqlInfo = property.SqlServer();
};
return result;
}

关于c# - EF Core - 字符串或二进制数据将被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56117925/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com