bra*_*w98 6 c# entity-framework asp.net-mvc-scaffolding asp.net-core 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>();
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
在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?}");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常,直到我想用控制器和视图使用EF来构建模型.设置好所有内容并单击"确定"后,我收到一条错误消息:找到了多个名为"ShareAndCare.Models.ShareAndCareContext"的DbContext.通过使用其确切大小写提供其完全限定名称来指定要使用的名称.我检查了所有的文件夹和命名空间,但那里没有问题,只有一个具有该名称的上下文.那问题是什么?
bra*_*w98 17
我要离开这个问题并在这里回答所以人们不要疯狂地像我一样手动寻找所有可能的解决方案.我发现在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?}");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1783 次 |
| 最近记录: |