2016-03-08 16 views
1

我有以下代碼:給定的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

回答

0

您的CustomerType是數據庫中的一個int,它不在代碼中,請嘗試將其轉換爲int。

0

來源不含意味着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

​​
相關問題