简体   繁体   中英

The given ColumnName does not match up with any column in data source

I have the following code:

public void BulkInsert(IEnumerable<T> items)
{
    var sbCopy = new SqlBulkCopy(_dataContext.Database.Connection.ConnectionString) { BulkCopyTimeout = 60 * 10 };

    var tablename = _dbset.GetTableName();
    sbCopy.DestinationTableName = tablename;
    foreach (PropertyInfo propertyInfo in items.ElementAt(0).GetType().GetProperties())
    {
        sbCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(propertyInfo.Name, propertyInfo.Name));
    }
    sbCopy.WriteToServer(items.AsDataReader());
}

The items list looks like this:

在此处输入图片说明

(notice the CustomerType field)

Also, the database the field. 该字段。

在此处输入图片说明

The mapping between the two present:

在此处输入图片说明

However, when executing the .WriteToServer() , I get the following exception

{"The given ColumnName 'CustomerType' does not match up with any column in data source."}

您的CustomerType在数据库中是一个int,不在代码中,请尝试将其强制转换为int。

The Source does not contain means the IEnumerable<T> items .. does not contain ColumnName CustomerType ... It is possible of having fault in .AsDataReader() function ...

I prefer to use ToDatatable rather ..

public static class ExtensionHelper
{

    public static System.Data.DataTable ToDataTable<T>(this IEnumerable<T> data)
    {
        var AllProperty = (typeof(T)).GetProperties().ToList();

        //var AllInsteadOf = InsteadOfProperty.Split('|').SkipWhile(i => string.IsNullOrEmpty(i));

        int propcount = AllProperty.Count();

        var table = new System.Data.DataTable();

        for (int i = 0; i < propcount; i++)
        {
            var prop = AllProperty[i];
            if (prop.CanRead)
            {
                //If property type is Nullable<T> or It's a string type or it's a custom class theb
                //column allowDBNull will be true
                bool allowDBNull = false;

                if ((prop.PropertyType.IsNullableType()) || (prop.PropertyType == typeof(String)) || (prop.PropertyType.IsCustomClass()))
                {
                    allowDBNull = true;
                }

                table.Columns.Add(prop.Name, prop.PropertyType.GetCoreType()).AllowDBNull = allowDBNull;
            }
        }

        object[] values = new object[propcount];
        foreach (T item in data)
        {
            for (int i = 0; i < values.Length; i++)
            {
                var prop = AllProperty[i];
                if (prop.CanRead)
                {
                    values[i] = prop.GetValue(item, null);
                }
            }
            table.Rows.Add(values);
        }
        return table;
    }
    public static bool IsNullableType(this Type Value)
    {
        return (Value.IsGenericType && Value.GetGenericTypeDefinition() == typeof(Nullable<>));
    }

    public static bool IsCustomClass(this Type Value)
    {
        return !Value.Namespace.StartsWith("System");
    }
    /// <summary>
    /// Get underlying core type of a nullable data type
    /// </summary>
    /// <param name="Value">The value.</param>
    /// <returns>A Type object</returns>
    public static Type GetCoreType(this Type Value)
    {
        Type u = Nullable.GetUnderlyingType(Value);
        return u ?? Value;
    }
}

And then

sbCopy.WriteToServer(items.ToDataTable());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM