So I've been getting this error when I try to run my project... The project was sent to me by one of my group members.
" Cannot open database "Identity" requested by the login. The login failed. Login failed for user "
The project itself is a web application using MVC. We have a database for a list of clubs/players. All I am trying to do currently is just to run it to see what my group has completed, but it keeps showing me the error above. I've searched online extensively to no avail which is why I am posting here to seek help.
The following are the:
IdentitySeedData.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
namespace P0.Models
{
public static class IdentitySeedData
{
private const string adminUser = "Admin";
private const string adminPassword = "Secret123$";
public static async void EnsurePopulated(IApplicationBuilder app)
{
UserManager<IdentityUser> userManager = app.ApplicationServices
.GetRequiredService<UserManager<IdentityUser>>();
IdentityUser user = await userManager.FindByNameAsync(adminUser);
if (user == null)
{
user = new IdentityUser("Admin");
await userManager.CreateAsync(user);
}
}
}
}
AppIdentityContext.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace P0.Models
{
public class AppIdentityDbContext : IdentityDbContext<IdentityUser>
{
public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options)
: base(options) { }
}
}
Program.cs
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace P0
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseDefaultServiceProvider(options =>
options.ValidateScopes = false)
.Build();
}
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using P0.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
namespace P0
{
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.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration["Data:ConnectionStrings:SoccerClubs"]));
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
Configuration["Data:ConnectionStrings:AppIdentityDbContext"]));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<IClubRepository, EFClubRepository>();
services.AddTransient<IPlayerRepository, EFPlayerRepository>();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
SeedData.EnsurePopulated(app);
IdentitySeedData.EnsurePopulated(app);
}
}
}
appsetting.json
{
"Data": {
"ConnectionStrings": {
"SoccerClubs": "Server=(localdb)\\MSSQLLocalDB;Database=SoccerClubs;Trusted_Connection=True;MultipleActiveResultSets=true",
"AppIdentityDbContext": "Server=(localdb)\\MSSQLLocalDB;Database=Identity;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}