给定的ColumnName不与数据源的任何列匹配

问题描述:

我有以下代码:给定的ColumnName不与数据源的任何列匹配

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

items名单如下:

enter image description here

(注意CustomerType场)

另外,数据库确实有这个字段。

enter image description here

两个之间的映射是本:

enter image description here

但是,执行.WriteToServer()时,出现以下异常

{“给定的ColumnName'CustomerType'与数据源中的任何列不匹配。“ }

+0

是'T'' BulkProduct'还是一些接口或超类型?因为您将映射基于枚举中第一个元素的特定具体类型,并且如果它与'T'不同,可能会出现一些类型不匹配。 –

+0

你正在使用哪个版本的EF? – Moumit

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

来源不含意味着IEnumerable<T> items ..不包含的ColumnName CustomerType ......这是可能具有.AsDataReader()功能故障...

我更喜欢使用ToDatatable而..

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

而且then

​​