ObjectContext实例已在InRequestScope中处理!
我在网上尝试了几个小时试图解决问题.
ObjectContext实例已被释放,不能再用于需要连接的操作.
我发现了几个文章和帖子中包含了同样的问题这个,这个,这个和这个
我尝试了所有方法,但总是发生错误.
上下文
public class BindSolutionContext : DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<ProjectImage> ProjectImages { get; set; }
public BindSolutionContext()
: base("name=Data")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BindSolutionContext>());
}
}
Run Code Online (Sandbox Code Playgroud)
Ninject
kernel.Bind<BindSolutionContext>().ToSelf().InRequestScope();
kernel.Bind<IProjectRepository>().To<ProjectRepository>().InRequestScope();
kernel.Bind<IUserRepository>().To<UserRepository>().InRequestScope();
kernel.Bind<IRoleRepository>().To<RoleRepository>().InRequestScope();
kernel.Bind<IAddressRepository>().To<AddressRepository>().InRequestScope();
kernel.Bind<IProjectImageRepository>().To<ProjectImageRepository>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
知识库
public class ProjectRepository : IProjectRepository
{
private readonly …Run Code Online (Sandbox Code Playgroud) dependency-injection ninject custom-membershipprovider asp.net-mvc-3 dbcontext
为什么我们能够使用自动属性DBSet,但不能使用ObjectSet:
public class SomeContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在为一家电信公司开发一个大型系统.我是DDD的新手,很难将不同的部分连接在一起.我们当前的系统是使用NHibernate构建的.它目前有超过600个表,所有数据访问都是使用NHibernate完成的,但对于新系统,我们将使用EF.以下是每个功能区域中的几个功能区域和数据库表的示例.
客户
-----> CustomerDemographics
-----> CustomerPayments
-----> CustomerTransactions
RoutingEngine
-----> InboundRoutes
-----> OutboundRoutes
ProvisioningEngine
-----> InboundSwithces
-----> OutboundSwitches
-----> RouterConfigs
-----> GatewayConfigs
BillingEngine
-----> InboundTraffic
-----> OutboundTraffic
由于系统必须是可单元测试的,因此我开始使用存储库模式抽象实际实体.一种方法是为每个数据库表创建一个存储库对象.当然,所有这些存储库类都可以从通用存储库接口派生.然而,这将在代码库维护方面增加相当多的开销.在DDD中,我读到了聚合的这个概念,但我不确定它应该如何在EF的上下文中特别应用.Aggregate对象应该是这些存储库的容器还是更多的相关上下文的容器(意味着有界DbContexts的内容)?
domain-driven-design entity-framework aggregate repository-pattern dbcontext
这可能是一个显而易见的问题......下面是我用于与数据库交互的静态类的框架.我的问题是:如果一个静态类在应用程序的持续时间内存在,那么这是否意味着字段_context将具有一个长时间保持打开状态的连接?或者如果我使用using包装我所做的调用声明,我可以确信连接只会按预期打开和关闭吗?
public static class MyStaticClass
{
private static dbEntities _context;
static MyStaticClass()
{
_context = new dbEntities();
}
private static void UpdateContext()
{
_context = new dbEntities();
}
public static bool DoSomething(int id)
{
using (var context = _context)
{
var result = (from x in context.table.where(p=>p.id == id) select x).FirstOrDefault();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是Entity Framework Code的新手,我正在构建一个小应用程序来习惯它.当网站第一次运行时,我访问数据库中的现有目录值,并使用razor在下拉列表中显示.
public void GetCats()
{
using (context = new RecipeContext())
{
try
{
var query = (from r in context.Catalogues
select r).Distinct().ToList();
catalogues = query.Select(t => t.CatalogueName.ToString()).ToList();
catalogues.Sort();
}
catch (Exception exe)
{
labMessage = exe.Message;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试将Catalog值添加到上下文时,我得到了上述错误.
public void AddCatalogue(string catalogueName)
{
using(context = new RecipeContext())
{
try
{
catalogueName = catalogueName.ToLower();
var catalogue = new RecipeCatalogue { CatalogueName = catalogueName };
if (context.Catalogues.Where(t => t.CatalogueName == catalogueName).Count() > 0)
{
labMessage = "The value …Run Code Online (Sandbox Code Playgroud) 我已经使用ASP.NET Web窗体和Entity Framework 6开发了一个Web应用程序。
我已经读到我应该为每个请求实例化一个新的DbContext实例(使用“ using”键),而不是尝试将其持久化。
现在,在所有需要查询数据库的地方,我都将实例化DbContext:
protected void Page_Load(object sender, EventArgs e)
{
using(var tmcc = new TMCContext())
{
// querying tmcc entities and/or update them
// ....
tmcc.SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果两个人同时加载页面怎么办?这意味着将有两个并发的DbContext实例。这不是问题吗?
感谢您的回答。
我有一个包含多个图层的实体层次结构,其中一个图层包含的对象数量可以达到数十万个.有时我只想要顶级对象,但我发现实体框架正在加载层次结构中的所有内容.
我甚至尝试过明确的延迟加载,但无济于事.
using (var db = new MyEntities())
{
db.Configuration.ProxyCreationEnabled = true;
db.Configuration.LazyLoadingEnabled = true;
var daoDict = (from d in db.stt_dictionary
where d.id == dictionaryID && !d.deleted
select d).FirstOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
在调试时,如果我单步执行此操作然后将鼠标悬停在上面,daoDict我会发现其集合属性(virtual包含数千个对象).
为什么?
我正在使用具有自己上下文的 Identity。
public class ApplicationUser : IdentityUser {
// some custom fields
}
public class IdentityContext : IdentityDbContext<ApplicationUser> {
//...
}
Run Code Online (Sandbox Code Playgroud)
我还有其他一些像这样的实体
public class Comment{
public int Id {get;set;}
public string Message{get;set;}
public DateTime Time{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我的其他上下文使用的
public class MyContext :DbContext {
public DbSet<Comment> Comments { get; set; }
//... other DbSets
}
Run Code Online (Sandbox Code Playgroud)
题。我希望我的 Comment 实体具有作者属性,所以我会有类似的东西
public class Comment{
public int Id {get;set;}
public string Message{get;set;}
public DateTime Time{get;set;}
public virtual ApplicationUser Author {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
但是 ApplicationUser 位于不同的上下文中,尽管在同一个数据库中。我打赌这是不可能的。
如何正确实现这一点?我应该将 DbSets …
我正在尝试使用SQLite数据库创建一个asp.net核心2.1应用程序。这是我第一次使用SQLite。我在尝试搭建控制器并从模型查看时遇到错误。
Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Doctor'
Method 'Clone' in type 'Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqliteOptionsExtension' from assembly 'Microsoft.EntityFrameworkCore.Sqlite, Version=1.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation. StackTrace:
at Microsoft.EntityFrameworkCore.SqliteDbContextOptionsBuilderExtensions.UseSqliteMethod 'Clone' in type 'Microsoft.EntityFrameworkCore.Infrastructure.Internal.SqliteOptionsExtension' from assembly 'Microsoft.EntityFrameworkCore.Sqlite, Version=1.1.6.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
at PatientRecord.Startup.<>c.<ConfigureServices>b__4_1(DbContextOptionsBuilder options)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass1_0`2.<AddDbContext>b__0(IServiceProvider p, DbContextOptionsBuilder b)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.DbContextOptionsFactory[TContext](IServiceProvider applicationServiceProvider, Action`2 optionsAction)
at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.<>c__DisplayClass10_0`1.<AddCoreServices>b__0(IServiceProvider p)
at …Run Code Online (Sandbox Code Playgroud)sqlite dbcontext asp.net-mvc-scaffolding entity-framework-core asp.net-core-2.1
我使用c#,ASP.NET Core,EF.
我有Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
...
}
Run Code Online (Sandbox Code Playgroud)
我有控制器:
public class HomeController : Controller
{
public AppDbContext _db;
public HomeController(AppDbContext db)
{
_db = db;
}
public IActionResult Index()
{
// _db is NOT NULL
var d = _db.Users.FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
我有MyService.cs类:
public class MyService
{
public static AppDbContext _db;
public MyService(AppDbContext db)
{
_db = db;
}
public static void GetOtherValue()
{
// _db is NULL
var d = _db.Users.FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController正常工作: …
dbcontext ×10
c# ×6
asp.net-mvc ×2
aggregate ×1
asp.net ×1
asp.net-core ×1
concurrency ×1
lazy-loading ×1
linq ×1
ninject ×1
sqlite ×1
startup ×1