如何解决.Net EF Identity 表未显示在数据库中获取无效的对象名称“AspNetUsers”异常
我正在尝试对我的 webapi 进行登录调用,这导致以下异常。身份表未显示在 Microsoft sql Server 中。我试过使用迁移/更新数据库命令。
我还尝试制作第二个项目,其中我首先添加了 EF,但当我开始添加其他表时,它们不会出现。
这是我在 stackoverflow 上的第一篇文章,如果不让我知道,我希望它符合标准,我将编辑/添加需要的内容。
控制器登录:
is:unresolved age:-1w
ApplicationDbContext:
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult<String>> Createtoken(LoginDTO model)
{
var user = await _userManager.FindByIdAsync(model.Email);
if (user != null)
{
var result = await _signInManager.CheckPasswordSignInAsync(user,model.Password,false);
if (result.Succeeded)
{
string token = GetToken(user);
return Created("",token); //returns only the token
}
}
return BadRequest();
}
启动
public class TicketContext : IdentityDbContext<IdentityUser> {
public TicketContext(DbContextOptions<TicketContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Ticket>()
.HasMany(p => p.Artists)
.WithOne()
.Isrequired()
.HasForeignKey("TicketId"); //Shadow property
modelBuilder.Entity<Ticket>().Property(t => t.Name).Isrequired().HasMaxLength(50);
modelBuilder.Entity<Artist>().Property(t => t.Name).Isrequired().HasMaxLength(50);
modelBuilder.Entity<Customer>().Property(c => c.FirstName).Isrequired().HasMaxLength(50);
modelBuilder.Entity<Customer>().Property(c => c.LastName).Isrequired().HasMaxLength(50);
modelBuilder.Entity<Customer>().Property(c => c.Email).Isrequired().HasMaxLength(50);
/*modelBuilder.Entity<Customer>().Ignore(t => t.Purchases);
modelBuilder.Entity<Customer>().Ignore(t => t.Sales);*/
/* modelBuilder.Entity<CustomerPurchase>().HasKey(p => new { p.CustomerId,p.TicketId });
modelBuilder.Entity<CustomerPurchase>().HasOne(p => p.Customer).WithMany(u => u.Purchases).HasForeignKey(f => f.CustomerId);
modelBuilder.Entity<CustomerPurchase>().HasOne(p => p.Ticket).WithMany().HasForeignKey(p => p.TicketId);
modelBuilder.Entity<CustomerSale>().HasKey(p => new { p.CustomerId,p.TicketId });
modelBuilder.Entity<CustomerSale>().HasOne(p => p.Customer).WithMany(u => u.Sales).HasForeignKey(f => f.CustomerId);
modelBuilder.Entity<CustomerSale>().HasOne(p => p.Ticket).WithMany().HasForeignKey(p => p.TicketId);*/
//seeding the db
modelBuilder.Entity<Ticket>().HasData(
new Ticket { Id = 1,Name = "Eurovisiesongfestival",EventDate = new DateTime(2021,12,24) },new Ticket { Id = 2,Name = "TomorrowLand",EventDate = new DateTime(2022,2,14) });
//Shadow property can be used for the foreign key,in combination with anonymous objects
modelBuilder.Entity<Artist>().HasData(
new { Id = 1,Name = "Netsy",TicketId = 2 },new { Id = 2,Name = "Eminem",TicketId = 1 },new { Id = 3,Name = "Afro",new { Id = 4,Name = "Rihanna",new { Id = 5,Name = "Bruno Marz",TicketId = 1 }
);
}
public DbSet<Ticket> Tickets { get; set; }
public DbSet<Customer> Customers { get; set; }
}
例外:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<TicketDataInitializer>();
services.AddScoped<ITicketRepository,TicketRepository>();
services.AddScoped<ICustomerRepository,CustomerRepository>();
services.AddDbContext<TicketContext>(options =>
options.UsesqlServer(Configuration.GetConnectionString("TicketContext")));
services.AddIdentity<IdentityUser,IdentityRole>(cfg => cfg.User.RequireUniqueEmail = true).AddEntityFrameworkStores<TicketContext>();
services.Configure<IdentityOptions>(options =>
{ //Password settings.
options.Password.requiredigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.requiredLength = 6;
options.Password.requiredUniqueChars = 1;
//Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
//User settings;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMnopQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = true;
});
}
public void Configure(IApplicationBuilder app,IWebHostEnvironment env,TicketDataInitializer ticketDataInitializer)
{
//app.UseCors("AllowAllOrigins");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.USEOpenApi();
app.UseSwaggerUi3();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
ticketDataInitializer.InitializeData().Wait();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。