【问题标题】:Issue, VS 19 ASP.NET Core 3.1 when I start to create "MVC Controller with view, using Entity Framework"问题,VS 19 ASP.NET Core 3.1,当我开始创建“带有视图的 MVC 控制器,使用实体框架”
【发布时间】:2020-05-02 19:10:07
【问题描述】:

我正在使用 ASP.NET Core 3.1,我已经创建了控制器,它对我有用,几天后我需要为 ShoppingCartItem 创建一个控制器,此后我遇到了一些问题。

我的模型

namespace NCCMobileTest.Data.Model
{
    public class ShoppingCartItem
    {
        [Key]
        public int Id { get; set; }

        public string Name { get; set; }


        public decimal Price { get; set; }
        public int Quantity { get; set; }

        public string Category { get; set; }
        public string Color { get; set; }
        public string Brand { get; set; }
        public string Barcode { get; set; }

        // from Item table
        public int Item_Id { get; set; }
        public Customer customer { get; set; }

        // from generat bill no
        public int Bill_No { get; set; }
    }
}

我的DbContext

namespace NCCMobileTest.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
        {
        }

        public DbSet<ShoppingCartItem> ShoppingCartItems { get; set; }
    }
}

我的步骤截图

输出

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Could not get the reflection type for DbContext : NCCMobileTest.Data.ApplicationDbContext
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

【问题讨论】:

标签: c# entity-framework visual-studio-2019 dbcontext asp.net-core-3.1


【解决方案1】:

IdentityDbContext 继承自 DbContext

如果没有,您是否尝试过更改为

public class ApplicationDbContext : DbContext

【讨论】:

【解决方案2】:

最近我遇到了类似的问题。经过数小时的怀疑和调试,我明白问题在于我已将我的Startup 类重命名为StartUp,这导致了整个错误。

我不能确定这是否确实是您的问题,但值得检查一下。

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多