c# - 将强类型列表添加到数据库

标签 c# asp.net entity-framework asp.net-core

我正在尝试在 Controller 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; }
}

最佳答案

您需要编写以下代码。

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

关于c# - 将强类型列表添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45319448/

相关文章:

c# - PostgreSQL 错误 : String was not recognized as a valid date time

c# - 有没有办法可靠地检测 CPU 内核总数?

c# - 我是否正确回答了这个面试挑战? (在 C# 中检查有效的二叉树)

asp.net - 在 ASP 和 C# 中使用 RSS 提要

Javascript : DataTables.net,用延迟加载替换分页

c# - DDD - 如何在 EF Core 中跨有界上下文管理实体

c# - 将列表作为 SQL 表类型参数传递

c# - 在主机 ('Basic' 上配置的身份验证方案不允许在绑定(bind) 'BasicHttpBinding' 上配置的那些

c# - 您是否缺少 using 指令或程序集引用?

c# - SQL Server session 在应用程序关闭时保持打开状态