我是新手,asp.net正在努力學(xué)習(xí)。因此,我使用在線教程( https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio)中的代碼創(chuàng)建了一個(gè)項(xiàng)目以下課程??刂破鳎簄amespace WebApiSample.Controllers{ [Route("api/[controller]")] [ApiController] public class TodoController : ControllerBase { private readonly ToDoContext _context; public TodoController(ToDoContext context) { _context = context; if (_context.TodoItems.Count() == 0) { // Create a new TodoItem if collection is empty, // which means you can't delete all TodoItems. _context.TodoItems.Add(new ToDoItem { Name = "Item1" }); _context.SaveChanges(); } } // GET: api/Todo [HttpGet] public async Task<ActionResult<IEnumerable<ToDoItem>>> GetTodoItems() { return await _context.TodoItems.ToListAsync(); } // GET: api/Todo/5 [HttpGet("{id}")] public async Task<ActionResult<ToDoItem>> GetTodoItem(long id) { var todoItem = await _context.TodoItems.FindAsync(id); if (todoItem == null) { return NotFound(); } return todoItem; } // POST: api/Todo [HttpPost] public async Task<ActionResult<ToDoItem>> PostTodoItem(ToDoItem todoItem) { _context.TodoItems.Add(todoItem); await _context.SaveChangesAsync(); return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem); }您可以看到它不使用任何數(shù)據(jù)庫(kù)。如何在這個(gè)項(xiàng)目中添加一個(gè)數(shù)據(jù)庫(kù),以便每當(dāng)我運(yùn)行它時(shí),我都可以獲得之前已經(jīng)添加的項(xiàng)目列表?有任何想法嗎?
1 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
通常,實(shí)體框架將是最好的解決方案。下面是使用實(shí)體框架Entity Framework Databases Supported支持的數(shù)據(jù)庫(kù)的鏈接。但是,如果要連接到其他數(shù)據(jù)庫(kù),則需要進(jìn)行自己的CRUD 操作(創(chuàng)建、檢索、更新、刪除)。這是一個(gè)使用 MongoDB 的 CRUD示例。
- 1 回答
- 0 關(guān)注
- 126 瀏覽
添加回答
舉報(bào)
0/150
提交
取消