c# – 找到多个名为’NewProject.Models.DbContext’的DbContext通过使用精确案例提供其完全限定名称来指定要使用哪一个

c# – 找到多个名为’NewProject.Models.DbContext’的DbContext通过使用精确案例提供其完全限定名称来指定要使用哪一个,第1张

概述我正在开发一个使用Asp.Net Core 2.1的Web应用程序.在我用脚手架添加新标识后,它生成了以下代码: IdentityStartup.cs中生成的代码 [assembly:HostingStartup(typeof(ShareAndCare.Areas.Identity.IdentityHostingStartup))]namespace ShareAndCare.Areas.Ide 我正在开发一个使用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?}");            });                        }       }}
总结

以上是内存溢出为你收集整理的c# – 找到多个名为’NewProject.Models.DbContext’的DbContext通过使用精确案例提供其完全限定名称来指定要使用哪一个全部内容,希望文章能够帮你解决c# – 找到多个名为’NewProject.Models.DbContext’的DbContext通过使用精确案例提供其完全限定名称来指定要使用哪一个所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1221198.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存