Resolved ASP.NET CORE : unhandled SqlException: Invalid object name 'Users'.

Aaron.soggi92

Member
Joined
Oct 22, 2021
Messages
19
Programming Experience
Beginner
Hello, im currently getting to grips with ASP.NET Core and I've ended up having an issue with one of my functions within my UsersController class.

UsersController.cs

C#:
 public class UsersController : ControllerBase
    {
        private readonly IDBConnection _isqlConnection;
        private readonly DataContext _context; // here we have used dependency injection so that we can access the data from the DataContext class.
        public UsersController(DataContext context, IDBConnection connection)
        {
            _isqlConnection = connection;
            _context = context;
        }
 
        [HttpGet]
        public ActionResult<IEnumerable<AppUser>>GetUsers() // we are going to be returning a type of action result.
        {        
            return _context.Users.ToList();      
                 
        }

Startup.cs

C#:
private readonly IConfiguration _config;
        public Startup(IConfiguration configuration)
        {
            _config = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DataContext>(options =>
            {
                options.UseSqlServer(_config.GetConnectionString("DefaultConnection"));
            });
            services.AddScoped<IDBConnection, DBConnection>();
            services.AddHttpContextAccessor();
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
            });
        }

appsettings

C#:
{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Information",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  }
}

DBConnection.cs

C#:
    public interface IDBConnection
    {
        SqlConnection GetConnection();
     
    }
    public class DBConnection : IDBConnection
    {
        public SqlConnection con = new SqlConnection(@"Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DatingSite;Integrated Security=True");
        public SqlConnection GetConnection()
        {
            return con;
        }
    }

DataContext.cs

C#:
    public class DataContext : DbContext
    {
        public DbSet<AppUser> Users { get; set; }

        public DataContext(DbContextOptions options) : base(options)
        {
        }    
    }

AppUser.cs

C#:
    public class AppUser
    {
        public int Id { get; set; }
        public string Username { get; set; }
    }

Image of Error recieved

1634898331310.png



Does anyone have any Idea why this error might be occuring? Thanks
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
Looks like you marked the question as resolved. What what the resolution?
 
Top Bottom