2017-07-26 49 views
1

我試圖在控制器HTTP後期事件中向數據庫中添加一些條目。但是我收到以下錯誤:將強類型列表添加到數據庫

The entity type 'List' was not found. Ensure that the entity type has been added to the model.

代碼:

[HttpPost] 
[ValidateAntiForgeryToken] 
public async Task<IActionResult> Create([Bind("ID,Name,Weight)] List<WBLoading> wBLoading) 
{ 
    if (ModelState.IsValid) 
    { 
     _context.Entry(wBLoading).State = EntityState.Modified; 
     await _context.SaveChangesAsync(); 
     return RedirectToAction("Index"); 
    } 
    return View(wBLoading); 
} 

我已經加入該表到的DbContext:

public DbSet<Models.WBLoading> WBLoading { get; set; } 

型號:

public class WBLoading 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int Weight { get; set; } 
} 

回答

3

你需要編寫下面的代碼。

_context.WBLoading.add(wBLoading);  
await _context.SaveChangesAsync(); 
+1

即使你認爲你給出了正確的答案,你應該解釋它是如何工作來解決問題,而不是隻發佈代碼示例。 – AFract

+0

這給了我一個編譯錯誤:'錯誤\t CS1503 \t參數1:無法從'System.Collections.Generic.List '轉換爲'Ao.Models.WBLoading'' – Reafidy

+1

user _context.WBLoading。的AddRange(wBLoading);方法, – Koderzzzz