加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 找到多个名为’NewProject.Models.DbContext’的DbContext

发布时间:2020-12-15 23:50:41 所属栏目:百科 来源:网络整理
导读:我正在开发一个使用Asp.Net Core 2.1的Web应用程序.在我用脚手架添加新标识后,它生成了以下代码: IdentityStartup.cs中生成的代码 [assembly:HostingStartup(typeof(ShareAndCare.Areas.Identity.IdentityHostingStartup))]namespace ShareAndCare.Areas.Id
我正在开发一个使用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来构建模型.设置好所有内容并单击“确定”后,我收到一条错误消息:找到了多个名为“ShareAndCare.Models.ShareAndCareContext”的DbContext.通过使用其确切大小写提供其完全限定名称来指定要使用的名称.我检查了所有的文件夹和命名空间,但那里没有问题,只有一个具有该名称的上下文.那问题是什么?

解决方法

我要离开这个问题并在这里回答所以人们不要疯狂地像我一样手动寻找所有可能的解决方案.我发现在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,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?}");
            });                
        }   
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读