草庐IT

glx_context

全部标签

c# - 关于Entity Framework Context Lifetime的问题

我对ASP.NETMVC应用程序中EntityFramework上下文的预期生命周期有一些疑问。在尽可能短的时间内保持上下文事件不是最好的吗?考虑以下Controller操作:publicActionResultIndex(){IEnumerablemodel;using(varcontext=newMyEntities()){model=context.MyTable;}returnView(model);}上面的代码将不起作用,因为EntityFramework上下文在View呈现页面时超出了范围。其他人会如何构造上面的代码? 最佳答案

c# - 使用 Entity Framework 添加具有读/写操作和 View 的 Controller - "Data Context class"是什么?

所以在VisualStudio中,当我去添加一个Controller时,我得到这个对话框:我很好奇如果我在使用EF时选择“具有读/写操作和View的Controller,使用EntityFramework”,VisualStudio会创建什么。所以我将我的模型类设置为创建的View模型,为我的View选择了Razor,但我不知道数据上下文类是什么?下拉列表中唯一的内容是我创建的View模型。 最佳答案 DataContext类是顶级EF对象,它包含映射到数据库表的所有实体集合。Controller需要能够访问它,以便它可以执行CRU

c# - Entity Framework 核心 : Log queries for a single db context instance

使用EFCore(或与此相关的任何ORM)我想跟踪ORM在我的软件中的某些操作期间对数据库进行的查询次数。我之前在Python下使用过SQLAlchemy,在那个堆栈上,这很容易设置。我通常有单元测试,这些单元测试针对内存中的SQLite数据库断言针对场景进行的查询数量。现在我想使用EFCore做同样的事情,并查看了Loggingdocumentation.在我的测试设置代码中,我按照文档所述进行操作:using(vardb=newBloggingContext()){varserviceProvider=db.GetInfrastructure();varloggerFactory=

c# - 无效操作异常 : Cannot create a DbSet for 'Role' because this type is not included in the model for the context

以下解决方案适用于.netcore1.1,但从1.1升级到2.0后,我收到以下错误:InvalidOperationException:CannotcreateaDbSetfor'Role'becausethistypeisnotincludedinthemodelforthecontext.当用户尝试登录并执行以下语句时:varresult=await_signInManager.PasswordSignInAsync(model.Email,model.Password,model.RememberMe,lockoutOnFailure:false);怎么了?User.cspubl

C# 编译器 : cannot access static method in a non-static context

我有下面的代码:publicclassAnything{publicintData{get;set;}}publicclassMyGenericBase{publicvoidInstanceMethod(Tdata){//dosomejob}publicstaticvoidStaticMethod(Tdata){//dosomejob}//othersmembers...}publicsealedclassUsefulController:MyGenericBase{publicvoidProxyToStaticMethod(){StaticMethod(null);}//others

c# - 如何防止 EF "The context cannot be used while the model is being created"错误?

查看我的Elmah错误日志,我看到一些来自EntityFramework的InvalidOperationException处理:Thecontextcannotbeusedwhilethemodelisbeingcreated.这是来自Nuget的最新EFCodeFirst库。我能够在网上找到的唯一信息是它是由将数据上下文作为单例引起的,这肯定不是我的情况。在我的Windsor安装程序中,我的EF工作单元结构正在注册:container.Register(Component.For().ImplementedBy().LifeStyle.PerWebRequest);我能够通过在VS

c# - Entity Framework 核心 : A second operation started on this context before a previous operation completed

我正在使用EntityFrameworkCore开发ASP.NetCore2.0项目在我的列表方法之一中出现了这个错误:InvalidOperationException:Asecondoperationstartedonthiscontextbeforeapreviousoperationcompleted.Anyinstancemembersarenotguaranteedtobethreadsafe.Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()这是我的方法:[Ht

c# - "Context cannot be used while the model is being created"异常与 ASP.NET 标识

为什么当我们调用AccountApiController.Register()方法时会发生这种情况?什么试图使用上下文?什么试图创建上下文?我们如何避免这种情况?我们如何调试它?"Message":"Anerrorhasoccurred.","ExceptionMessage":"Thecontextcannotbeusedwhilethemodelisbeingcreated.ThisexceptionmaybethrownifthecontextisusedinsidetheOnModelCreatingmethodorifthesamecontextinstanceisacces

c# - 仅 Entity Framework 代码错误 : the model backing the context has changed since the database was created

我使用EntityFramework4和CTP4创建了一个“仅限代码”的POCO,用于针对现有数据库。当我运行查询时出现错误Themodelbackingthe'xyzContext'contexthaschangedsincethedatabasewascreated.Eithermanuallydelete/updatethedatabase,orcallDatabase.SetInitializerwithanIDatabaseInitializerinstance.Forexample,theRecreateDatabaseIfModelChangesstrategywilla

c# - EF 5 启用迁移 : No context type was found in the assembly

我有4个项目:Toombu.Entities:allmodelsarethereToombu.DataAccess:Mapping,RepositoryandToombuContextToombu.Logique:LogicofmyapplicationToombu.Web:MVC4application.WithallothersDLL.我尝试在Toombu.Web中启用迁移,但出现此错误:Nocontexttypewasfoundintheassembly如何启用迁移? 最佳答案 令我惊讶的是,没有人提到这个问题的明显答案:En