I thought that I would learn to use ASP.NET Core, instead of what I am used to, ASP.NET, because Microsoft won't update ASP.NET anymore.
But I already got problems...
My problem is that I want to connect to my localdb file, and make this code-first table, but I get an error
No database provider has been configured for this DbContext.
even though I have a connection string for it:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=api;Trusted_Connection=True;"
},
And I do include it in the startup, that it should use that connection string:
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
My table class is like this:
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public int age { get; set; }
}
And here is my Context:
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
}
您的 UserContext 应该具有接受 DbContextOptions 并将其传递给基本构造函数的公共构造函数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.