2017-04-12 83 views
0

我有一個foreach循環遍歷類型列表List<NewItem>,創建一個新實例NewItem,設置其屬性,然後Add() s將項目添加到執行SaveChanges()時要插入的上下文:將多行插入到數據庫LINQ

foreach (var newItem in newItems) 
{ 
    NewItem item = new NewItem 
    { 
     User = newItem.User, 
     Itemno = newItem.Itemno 
    }; 

    db.NewItem.Add(item); 

} 

try 
{ 
    db.SaveChanges(); 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e); 
} 

我收到錯誤:

Unable to update the EntitySet 'NewItem' because it has a DefiningQuery and no element exists in the element to support the current operation.

爲什麼不是Add()方法實際添加任何東西到我的分貝範圍內?

回答

1

如果目標數據庫表沒有主鍵,請添加一個。