尝试在 ASP.NET Core 6.0 MVC 中构建视图时出错

发布于 2025-01-18 23:31:52 字数 1152 浏览 0 评论 0 原文

我正在尝试使用Visual Studio脚手架新的剃须刀视图。我选择一个模板,我的模型和 dbContext ,然后获取下面显示的错误消息。

值得注意的事情。我的模型,我的 dbContext 和我的网站都在不同的项目中。从下面的消息中,我正在使用 adddbContext ,并且有一个构造函数,该构造函数接受 dbContextOptions< tcontext> parameter。

我在博客文章上读了一条评论,即问题是因为我的上下文在另一个项目中。该注释引用了有关将配置注入 dbcontext 的需要的内容以获取连接字符串并在 on Conconfiguring 覆盖中手动添加。

如果这是正确的或如何进行设置,我找不到任何示例。任何帮助将不胜感激。

EDIT: 从上面提到的博客评论中测试理论,我将本节添加到了我的dbcontext中。 ConnectionsTring 是一个硬编码的字符串常数,其中包含我的连接信息。这确实有效并让我脚手架,因此问题仍然存在。如何将此连接字符串注入我的dbcontext以允许脚手架工作?

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
         optionsBuilder.UseSqlServer(ConnectionString);
    }
    else
    {
         base.OnConfiguring(optionsBuilder);
    }
}

编辑:因此,在进行此更改之后,我检查了代码,并让另一个开发人员选择了它。似乎上面的本节只需要在那里即可允许脚手架工作。他从未将连接字符串更改为他的环境。他不再遇到错误的错误。

I'm trying to scaffold a new razor view using Visual Studio. I select a template, my model and my DbContext, then I get the error message shown below.

Things to note. My models, my DbContext and my website are all in different projects. From the message below I am using AddDbContext and I have a constructor that accepts a DbContextOptions<TContext> parameter.

I read a comment on a blog post that the issue is because my context is in another project. The comment referenced something about the need to inject the Configuration into the DbContext to get the connection string and manually add it in the OnConfiguring override.

I can't find any examples if this is correct or how to set it up. Any help would be appreciated.

Error Message

EDIT:
Testing out the theory from the blog comment I mentioned above, I added this section into my DbContext. ConnectionString is a hardcoded string constant with my connection information. This does work and allow me to scaffold, so the question still remains. How can I inject this connection string into my DbContext to allow the scaffolding to work?

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
         optionsBuilder.UseSqlServer(ConnectionString);
    }
    else
    {
         base.OnConfiguring(optionsBuilder);
    }
}

EDIT: So after making this change, I checked in the code and had another developer pick it up. It appears this section above just needs to be there to allow scaffolding to work. He never changed the connection string to point to his environment. He no longer got the error above it just worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七度光 2025-01-25 23:31:52

我不确定实际问题是什么,但似乎我们在设计时创建 DbContext 时遇到了问题。我手动添加了下面的代码,现在可以使用了。这只是一个临时解决方案。

   public AppDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=JwtTemplate;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
            return new AppDbContext(optionsBuilder.Options);
        }

参考:https://stackoverflow.com/a/70559350

I am not sure about what is the actual problem but it seems like we were having problems creating DbContext at design time. I manually added the code below and it's working now. It's just a temporary solution tho.

   public AppDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=JwtTemplate;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
            return new AppDbContext(optionsBuilder.Options);
        }

Reference: https://stackoverflow.com/a/70559350

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文