EF Core - строка или двоичные данные будут обрезаны - PullRequest
0 голосов
/ 13 мая 2019

Как вы определяете, какой столбец является виновником, когда у вас есть 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;
}

Приведенный выше код вызывается этой частью catch команды try /ловить вокруг db.SaveAsync ();оператор.

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;
}

Итак, суть вопроса такова: как получить определение столбца SQL из EF Core?

Я хочу иметь возможностьзарегистрируйте конкретную таблицу и столбец , когда 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);
        }
    }
}

1 Ответ

0 голосов
/ 14 мая 2019

Как уже упоминалось в комментариях, вам нужно полное имя, и это можно прочитать из метаданных.

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...