After the comparasion with the code and configuration in the project, it seemed no error in the project, so OP tried to create a new .net 5 project and this time everything performing like a charm. Maybe it can be a tip for similar issue, that is changing the version of the framework of the project.
===================================================
Google the error message I really found that the high voted answer asked to clear Nuget caches but as you said it not working. I'm afraid I need your help you check where's the differences between yours and mine because it worked in my side. By the way, using v3.1.18 Microsoft.EntityFrameworkCore.SqlServer also worked for me, so my guess it may result from your visual studio client. You may reinstall the client.
I newly created an asp.net core 3.1 api project, and installed ef:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.9" />
</ItemGroup>
</Project>
And in the startup, I modified my ConfigureService method:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<LocaldbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Localtest")));
}
And this is a test controller:
using WebApplication1.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Controllers
{
public class LocaldbController : Controller
{
private readonly LocaldbContext _context;
public LocaldbController(LocaldbContext context)
{
_context = context;
}
[HttpGet("/getUser")]
public async Task<IActionResult> Index(string searchString)
{
var users = from m in _context.user
select m;
if (!String.IsNullOrEmpty(searchString))
{
users = users.Where(s => s.user_name.Contains(searchString));
}
return Ok(await users.ToListAsync());
}
}
}
And Dbcontext file:
using WebApplication1.Models;
using Microsoft.EntityFrameworkCore;
namespace WebApplication1.Data
{
public class LocaldbContext: DbContext
{
public LocaldbContext(DbContextOptions<LocaldbContext> options) : base(options){}
public DbSet<LocalMallUser> user { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<LocalMallUser>().ToTable("MallUser");
}
}
}