gpt4 book ai didi

entity-framework - 在 MVC "No parameterless constructor defined for this object."中搭建读取操作的脚手架

转载 作者:行者123 更新时间:2023-12-04 02:52:10 24 4
gpt4 key购买 nike

我正在开发一个基本的 MVC5/EF6 应用程序,当我尝试在 MVC 中构建读取操作时遇到以下错误:

Error 

There was an error running the selected code generator:
'No Parameterless constructor defined for this object'

无论如何它都不需要它,因为我调用的是读取而不是删除或更新,但是有问题的模型确实有一个无参数的构造函数(就像它下面的模型一样)。
public class Article
{
public int ArticleID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime PublishDate { get; set; }

public virtual Author Author { get; set; }

public Article()
{
}
}

我的 Controller 在下面,它还有一个无参数的构造函数:
public ArticleController()
{
connection = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
context = new TRNContext(connection);
}

// GET: Article
public ActionResult Index(int id)
{
return View(context.Articles.SingleOrDefault(a => a.ArticleID == id));
}

最佳答案

由于无法连接到数据库,我在 ASP.NET Core MVC 应用程序上遇到了这个错误。我花了几个小时检查我的模型和 dbContext 类才意识到它们没有任何问题。应用程序只是无法连接到数据库。有两个地方我必须检查。

  • Startup.cs - 应使用正确的连接字符串注册数据库上下文。
    enter image description here
  • Appsetting.json - 应该正确输入连接字符串。
    enter image description here
  • 关于entity-framework - 在 MVC "No parameterless constructor defined for this object."中搭建读取操作的脚手架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844874/

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