[英]Entity Framework Core database
我以為我會學習使用 ASP.NET Core,而不是我習慣的 ASP.NET,因為 Microsoft 不會再更新 ASP.NET。
但是我已經有問題了...
我的問題是我想連接到我的 localdb 文件,並制作這個代碼優先表,但我收到一個錯誤
尚未為此 DbContext 配置數據庫提供程序。
即使我有一個連接字符串:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=api;Trusted_Connection=True;"
},
我確實將它包含在啟動中,它應該使用該連接字符串:
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
我的表類是這樣的:
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public int age { get; set; }
}
這是我的上下文:
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
}
您的 UserContext 應該具有接受 DbContextOptions 並將其傳遞給基本構造函數的公共構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.