gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:56:05 25 4
gpt4 key购买 nike

我正在尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com