小编mas*_*one的帖子

ArgumentException:调用 'DbSet<News>.Find' 的位置 0 处的键值是 'string' 类型,与 int' 的属性类型不匹配

当我尝试编辑我的帖子时,会引发此错误。

这是我的代码:

public async Task<IActionResult> Edit(string id)
{
    if (id == null)
    {
        return NotFound();
    }
    // issue 
    var news = await _context.News.FindAsync(id);
    if (news == null)
    {
        return NotFound();
    }
    return View(news);
}
Run Code Online (Sandbox Code Playgroud)

调试器将代码停止在

var news = await _context.News.FindAsync(id);
Run Code Online (Sandbox Code Playgroud)

我的模型的代码是

public int id { get; set; }
[Required(ErrorMessage = "Enter your name.")]
public string Author { get; set; }
[Required(ErrorMessage = "Enter the title.")]
public string Title { get; set; }
[Required(ErrorMessage = "Enter the issued date.")]
[DataType(DataType.Date)]
public …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core asp.net-core-mvc asp.net-core

6
推荐指数
1
解决办法
7491
查看次数