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

时间:2019-05-13 18:24:56

标签: c# entity-framework-core

当您有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

时记录特定的

2 个答案:

答案 0 :(得分:2)

在 .NET Core 3.1 和 EFCore 5.0.2 上,此日志记录无需额外的扩展方法即可工作:

try
{
    await context.SaveChangesAsync();
}
catch(Exception ex)
{
    foreach (var entityEntry in context.ChangeTracker.Entries().Where(et => et.State != EntityState.Unchanged))
    {
        foreach (var entry in entityEntry.CurrentValues.Properties)
        { 
            var prop = entityEntry.Property(entry.Name).Metadata;
            var value = entry.PropertyInfo?.GetValue(entityEntry.Entity);
            var valueLength = value?.ToString()?.Length;
            var typemapping = prop.GetTypeMapping();
            var typeSize = ((Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping) typemapping).Size;
            if (typeSize.HasValue && valueLength > typeSize.Value)
            {
                Log.Error( $"Truncation will occur: {entityEntry.Metadata.GetTableName()}.{prop.GetColumnName()} {prop.GetColumnType()} :: {entry.Name}({valueLength}) = {value}");
            }
        }
    }
    throw ex;
}

答案 1 :(得分:0)

如评论中所述,您需要全名,并且可以从元数据中读取。

         <div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
    <form>
      <div class="form-row">
        <div class="col-0.5 search-label">
          <label class="control-label">Search :</label>
        </div>
        <div class="col-sm-4 col-md-3 col-lg-3 col-xl-3">
          <input type="text" v-model="company_name" @keyup="userFilterGlobal" class="form-control" placeholder="Company">
        </div>
        <div class="col-sm-4 col-md-3 col-lg-3 col-xl-3">
          <input type="text" v-model="fullname" @keyup="userFilterGlobal" class="form-control" placeholder="Fullname">
        </div>
        <div class="col-sm-4 col-md-3 col-lg-3 col-xl-3">
          <input type="text" v-model="email" @keyup="userFilterGlobal" class="form-control" placeholder="Email">
        </div>
      </div>
    </form>
  </div>