嘿,我将 VS2022 更新到了最新版本,当我创建一个新的 MVC 项目时,我找不到启动文件,只有 program.cs 找到了?正常吗?以及如何在程序中添加此代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
欢迎各位兄弟 发布技术文章
这里的技术是共享的
见下面红色的部分
嘿,我知道您说您只是使用了 .NET 5.0,但对于使用 .NET 6.0 运行的任何人来说
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
您需要做的就是添加以下变量
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
当你调用函数时
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
你应该没有问题
在 .Net 6.0 中 program.cs 和 startup.cs 已经统一。您可以查看docs.microsoft.com中的文档
您应该能够像在 startup.cs 中一样插入上下文。祝你好运!
来自 https://stackoverflow.com/questions/68973067/startup-cs-file-in-asp-net-mvc-is-not-shown
AddDbContext()
这样),您需要在项目中注册服务的地方。这通常在 Startup.cs 中完成。根据提供的信息,无法了解您的项目的外观,因此无法回答此问题。最简单的可能性是,您选择了错误的项目类型并创建了控制台应用程序。我只是用一个新项目再试一次。