C# DBContext的正确使用
我有一个多用户可以登录的应用程序。为此,我有一种会话对象,它存储一个C# DBContext的正确使用,c#,entity-framework,dbcontext,C#,Entity Framework,Dbcontext,我有一个多用户可以登录的应用程序。为此,我有一种会话对象,它存储一个DBContext对象。但问题是,缓存的DBContext只存储登录用户的数据。当另一个用户也登录时,缓存的数据可能较旧,因为它将由第二个用户更改 有没有一个概念上的方法来处理这个问题。我可以在每次执行数据库请求时创建它,而不是缓存DBContext对象。这是正确的方法吗?还是有一种事件需要捕获才能知道数据库内容已更改?您不应该以任何方式缓存DbContext对象。DbContext在内部自动为多个用户维护状态 当您打开控制器以
DBContext
对象。但问题是,缓存的DBContext
只存储登录用户的数据。当另一个用户也登录时,缓存的数据可能较旧,因为它将由第二个用户更改
有没有一个概念上的方法来处理这个问题。我可以在每次执行数据库请求时创建它,而不是缓存
DBContext
对象。这是正确的方法吗?还是有一种事件需要捕获才能知道数据库内容已更改?您不应该以任何方式缓存DbContext对象。DbContext在内部自动为多个用户维护状态
当您打开控制器以响应用户数据请求时,您将创建一个新的上下文。在实体框架6中,这看起来像
public class FeedItemController : ApiController
{
private LynxFeedAPI_Context db = new LynxFeedAPI_Context();
// GET api/FeedItem
public IQueryable<FeedItem> GetFeedItems()
{
return db.FeedItems;
}
公共类FeedItemController:ApicController
{
私有LynxFeedAPI_上下文数据库=新的LynxFeedAPI_上下文();
//获取api/FeedItem
公共IQueryable GetFeedItems()
{
返回db.FeedItems;
}
在EF 7中,使用Startup.cs设置依赖项注入的方式有所不同
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();
services.Configure<AppSettings>(configuration.GetConfigurationSection("AppSettings"));
// Add EF services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration["Data:DefaultConnection:ConnectionString"]));
services.AddSingleton<IApplicationDbContext, ApplicationDbContext>();
services.AddSingleton<IProposalDataRepository, ProposalDataRepository>();
services.AddSingleton<IPositionDataRepository, PositionDataRepository>();
services.AddSingleton<IMandatoryReqDataRepository, MandatoryReqDataRepository>();
services.AddSingleton<IRatedReqDataRepository, RatedReqDataRepository>();
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
//取消对以下行的注释以添加Web API服务,这使得移植Web API 2控制器更加容易。
//您还需要将Microsoft.AspNet.Mvc.WebApiCompatShim包添加到project.json的“依赖项”部分。
//services.AddWebApiConventions();
services.Configure(configuration.GetConfigurationSection(“AppSettings”);
//将EF服务添加到服务容器中。
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>
使用SQLServer(配置[“数据:默认连接:连接字符串]);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
}
并由控制器使用
public class ProposalController : Controller
{
private readonly IProposalDataRepository repProposal;
public ProposalController(IProposalDataRepository repository) {
repProposal = repository;
}
[HttpGet]
public IEnumerable<Proposal> GetAll()
{
return repProposal.SelectAll();
}
公共类提议控制器:控制器
{
私有只读IProposalDataRepository;
公共提案控制员(IProposalDataRepository){
RepProposition=存储库;
}
[HttpGet]
公共IEnumerable GetAll()
{
返回repProposal.SelectAll();
}
如果不需要调用来创建新的DbContext,那么正确的方法是在我通过LINQ启动请求之前创建一个新的DbContext对象?是的,在打开控制器时创建一个新的上下文