c# - 找到多个名为 'NewProject.Models.DbContext' 的 DbContext 通过使用准确大小写的完全限定名称指定要使用的一个

标签 c# entity-framework asp.net-core asp.net-mvc-scaffolding asp.net-core-2.1

我正在使用 Asp.Net Core 2.1 开发 Web 应用程序。在我使用脚手架添加新身份后,它为我生成了这些代码:

IdentityStartup.cs 中生成的代码

[assembly:HostingStartup(typeof(ShareAndCare.Areas.Identity.IdentityHostingStartup))]
namespace ShareAndCare.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {

                services.AddDbContext<ShareAndCareContext>(options =>
                   options.UseLazyLoadingProxies().UseSqlServer(
                       context.Configuration.GetConnectionString("ShareAndCareContextConnection")));

                services.AddIdentity<ShareAndCareUser, IdentityRole>()
                .AddEntityFrameworkStores<ShareAndCareContext>()
                .AddDefaultTokenProviders();

                services.AddSingleton<IEmailSender, EmailSender>();               

            });

        }
    }
}

Startup.cs 中生成的代码

    namespace ShareAndCare
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }

            public IConfiguration Configuration { get; }

            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });            

                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);            
            }    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    app.UseHsts();
                }

                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseAuthentication();
                app.UseCookiePolicy();

                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });                
            }   
        }
    }

在我想使用 EF 搭建一个带有 Controller 和 View 的模型之前,它一直运行良好。设置所有内容并单击“确定”时,我收到一条错误消息:找到了多个名为“ShareAndCare.Models.ShareAndCareContext”的 DbContext。通过使用完全限定的大小写提供其完全限定名称来指定要使用的文件夹。 我检查了所有文件夹和 namespace ,但那里没有问题,只有一个上下文具有该名称。那么问题出在哪里?

最佳答案

我将问题和答案留在这里,这样人们就不会像我一样疯狂地手动寻找所有可能的解决方案。我发现在 IdentityHostingStartup.cs 的 Configure 方法中添加上下文是导致问题的原因。我更改了将上下文添加到 Startup.cs 的 Configure 方法的位置,它工作得很好。

namespace ShareAndCare
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });            

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddDbContext<ShareAndCareContext>(options =>
                   options.UseLazyLoadingProxies().UseSqlServer(
                       Configuration.GetConnectionString("ShareAndCareContextConnection")));

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseAuthentication();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });                
        }   
    }
}

关于c# - 找到多个名为 'NewProject.Models.DbContext' 的 DbContext 通过使用准确大小写的完全限定名称指定要使用的一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51318248/

相关文章:

c# - 我应该捕获并包装一般异常吗?

c# - WCF REST 服务 - 无法返回对象列表

c# - 嵌套随机生成器不是随机的

c# - 在 StackExchange.Redis 中通过键模式获取值

c# - 具有单独 ID 属性的 EF6 一对一关系

c# - 如何在 EF6 异步中启动实体存储过程而不等待返回?

c# - 运行 Windows 服务时拒绝访问

c# - Entity Framework 6 未使用属性正确映射我的列

asp.net-core - AspNetCore 中的 CreatePerOwinContext 替换是什么?

c# - 如何在泛型方法中使用 IOptions<T> 作为泛型参数