You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Razor Pages项目CS0229歧义错误原因咨询

Why am I getting CS0229 Ambiguity errors after scaffolding Razor Pages for Reviews in my ASP.NET Core app?

Great question—let's dig into why you're seeing those frustrating ambiguity errors and how to fix them.

The Core Issue: Namespace Collision

The problem boils down to duplicate class names in the same namespace. When you used the scaffolding tool to generate CRUD pages for both your Movie and Review models, it dumped all the generated page models (like CreateModel, IndexModel, EditModel) into the root RazorPagesMovie namespace instead of nesting them under their respective subfolder namespaces (RazorPagesMovie.Pages.Movies and RazorPagesMovie.Pages.Reviews).

In C#, if two classes have the exact same name and live in the same namespace, the compiler has no way to tell them apart. That's why you're seeing errors like Ambiguity between 'CreateModel._context' and 'CreateModel._context'—it's confused between the CreateModel from your Movies pages and the one from your Reviews pages.

Why Did the Scaffolder Do This?

By default, the ASP.NET Core scaffolding tool uses your project's root namespace for all generated code unless you explicitly tell it otherwise. It doesn't automatically infer the namespace from the folder structure where you're creating the pages, which leads to this collision when you add multiple CRUD sets in separate subfolders.

How to Fix It

You've got two solid options to resolve this:

  1. Retroactively Fix Namespaces

    • Go through each .cshtml.cs file in the Pages/Movies folder and change the namespace from RazorPagesMovie to RazorPagesMovie.Pages.Movies
    • Repeat this for every .cshtml.cs file in Pages/Reviews, updating the namespace to RazorPagesMovie.Pages.Reviews
    • Don't forget to update the @model directive in each corresponding .cshtml file to match the new fully qualified namespace (e.g., @model RazorPagesMovie.Pages.Reviews.CreateModel)
  2. Configure Scaffolding Correctly for Future Generations

    • Next time you run the scaffolding wizard, look for the "Namespace" option (usually tucked in the advanced settings) and set it to the sub-namespace corresponding to your target folder (like RazorPagesMovie.Pages.Reviews). This ensures the scaffolder generates code with the correct namespace right out the gate, avoiding collisions entirely.

Once you adjust those namespaces, the compiler will be able to tell your CreateModel (and other page model) classes apart, and those ambiguity errors should vanish.

内容的提问来源于stack exchange,提问作者Efthymios Kalyviotis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:38:12