.Net MVC 的新手。寻找一种在页面上显示 AspNetUser 表的内容以供授权用户查看的方法。创建了一个模型和一个带有 View 的 Controller 。问题是与该表的连接已经打开,我很难弄清楚如何引用该连接而不是创建一个新的 ApplicationDbContext() (这当然会返回错误。)请帮忙!代码如下:
Controller :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using PanoramaPoint.Models;
namespace PanoramaPoint.Controllers
{
public class AspNetUsersController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: AspNetUsers
public ActionResult Index()
{
return View(db.AspNetUsers.ToList());
}
如何引用现有的 ApplicationDbContext 而不是创建新的?
最佳答案
Entity Framework (和 ADO.Net,就此而言)使用所谓的连接池。当您请求新连接时,它实际上并没有出去并建立新连接。在幕后,它从已连接资源池中拉出现有连接。
您使用现有连接的请求由 Entity Framework 为您处理。别担心。您的代码就这样很好。
关于c# - .Net MVC : need to display a list of users from AspNetUser table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307121/