2017-07-19 84 views
0

我一直在试图将记录从一个表复制到另一个在实体框架MVC。这里是我的代码..无法将记录从一个表复制到另一个实体框架MVC

var records = _db.Attendance.Get(e => attIdsToDelete.Contains(e.Id)); 
    _db.AttendanceHistory.Insert(records); 

现在我已经越来越错误的记载,“不能从‘system.collections.generic.ienumerable’转换为‘attendancehistory’

列和类型的数量都在数据库中同一

+0

有要保存在'IEnumerable'所有记录?使用foreach循环和PE插入存储在其中的每个记录的插入。 –

+0

记录包含我要插入AttendanceHistory表中的记录列表(来自Attendance表)...现在它会引发上面提到的错误。 – ASK

+0

如何将数据从一张表存储到另一张表,而没有映射表或任何forloop –

回答

0

请把这个

_db.AttendanceHistory.Insert(records); 

此:

_db.AttendanceHistory.AddRange(records); 

如果考勤和考勤历史属于同一类型,这应该起作用。如果仍然抱怨,尝试铸造并改变列表第一:

var records = _db.Attendance.Get(e => attIdsToDelete.Contains(e.Id)).ToList<AttendanceHistory>(); 
0

您可以创建一个方法像下面,复制从源类的所有属性(值)到目的地类(如果有相同的属性都类(名和数据类型)

public class Utility 
{ 
    /// <summary> 
    /// Copy one class all properties to another class - if both the class having all the same properties 
    /// </summary> 
    /// <param name="FromClass">Class A - from where properties need to be copied</param> 
    /// <param name="ToClass">Class B - to whom properties need to be pasted</param> 
    public static void CopyAllPropertiesFromOneClassToAnotherClass(object FromClass, object ToClass) 
    { 
     foreach (PropertyInfo propA in FromClass.GetType().GetProperties()) 
     { 
      PropertyInfo propB = ToClass.GetType().GetProperty(propA.Name); 
      propB.SetValue(ToClass, propA.GetValue(FromClass, null), null); 
     } 
    } 
} 

然后在你的代码,你可以调用该方法将数据复制像下面

Utility.CopyAllPropertiesFromOneClassToAnotherClass(fromClassObject, toClassObject); 
相关问题