SqlException: Invalid object name 'AspNetUsers'

DAcharya

New member
Joined
Jun 7, 2023
Messages
1
Programming Experience
10+

SqlException: Invalid object name 'AspNetUsers'.​

  • Microsoft.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__188_0(Task<SqlDataReader> result)​

  • System.Threading.Tasks.ContinuationResultTaskFromResultTask<TAntecedentResult, TResult>.InnerInvoke()​

  • System.Threading.Tasks.Task+<>c.<.cctor>b__272_0(object obj)​

  • System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)​

  • System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)​

  • System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot, Thread threadPoolThread)​

  • Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)​

  • Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)​

  • Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)​

  • Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync<TState, TResult>(TState state, Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, CancellationToken cancellationToken)​

  • Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable<T>+AsyncEnumerator.MoveNextAsync()​

  • Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)​

  • Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)​

  • ESSWebPortal.Controllers.AuthController.Login() in AuthController.cs​

    +
    1. if (!await _context.Users.AnyAsync())
  • Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)​

  • System.Threading.Tasks.ValueTask<TResult>.get_Result()​

  • System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()​

  • Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask<IActionResult> actionResultValueTask)​

  • Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)​

 
If I had to guess, the OP also put equally low effort into reading or paying attention to the tutorial he is using. It looks like they forgot to initialize their database; OR they are mixing and matching code from the older version of ASP.NET identity framework with the newer version. But since the OP has given us so little information, it's hard to know.
 
hello i have the same issues ,
C#:
[Table("Users")]
public class ApplicationUser : IdentityUser
{
   
    [PersonalData]
    [Column(TypeName = "nvarchar(100)")]
    [Display(Name = "Prénom")]
    public string FirstName { get; set; }

    [PersonalData]
    [Column(TypeName = "nvarchar(100)")]
    [Display(Name = "Nom")]
    public string LastName { get; set; }

    [PersonalData]
    [Column(TypeName = "nvarchar(150)")]
    [Display(Name = "Pays")]
    public Country Country { get; set; }

    [PersonalData]
    [Display(Name ="Avatar")]
    public byte[] ProfileImage { get; set; }
}

error code:

Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'dbo.AspNetUsers'.
at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__209_0(Task`1 result)
at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
ClientConnectionId:37bb933b-cbbe-4e61-9e63-ab6e19a9a58d
Error Number:208,State:1,Class:16
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.
C#:
public MgiAuthContext(DbContextOptions<MgiAuthContext> options)
    : base(options)
{
}

public DbSet<MenuItem> MenuItems { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);

    builder.HasDefaultSchema("dbo");
    builder.Entity<IdentityUser>(entity =>
                                 {
                                     entity.ToTable(name:"Users");
                                 });
    builder.Entity<IdentityRole>(entity =>
                                 {
                                     entity.ToTable(name:"Roles");
                                 });
    builder.Entity<IdentityUserRole<string>>(entity =>
                                             {
                                                 entity.ToTable("UserRoles");
                                             });
    builder.Entity<IdentityUserClaim<string>>(entity =>
                                              {
                                                  entity.ToTable("UserClaims");
                                              });
    builder.Entity<IdentityUserLogin<string>>(entity =>
                                              {
                                                  entity.ToTable("UserLogins");
                                              });
    builder.Entity<IdentityRoleClaim<string>>(entity =>
                                              {
                                                  entity.ToTable("RoleClaims");
                                              });
    builder.Entity<IdentityUserToken<string>>(entity =>
                                              {
                                                  entity.ToTable("UserTokens");
                                              });

    builder.Entity<MenuItem>()
        .HasMany(m => m.Submenu)
        .WithOne()
        .HasForeignKey(m => m.ParentId);
}
 
Last edited by a moderator:
@tataf, as suggested earlier, it looks like you're mixing and matching ASP.NET Membership and ASP.NET Identity. The former is the old user management system for ASP.NET apps and should not be being used any more. AspNetUsers is a table in the schema required for that. Your code indicates that you are trying to use ASP.NET Identity, which was introduced some time ago as a replacement for Membership. I'm guessing that you are adding Membership as well as or instead of Identity in your Startup class. The OP may well be doing the same thing, although they've provided less information to go on.

BTW, please don't post unformatted code snippets. They are too hard to read. I have formatted your code for you this time. Please do it for us yourself in future.
 
Back
Top Bottom