简体   繁体   English

隔离EF Core中的更改

[英]Isolate Changes in EF Core

Here's how I add the the DbContext service in ASP.NET Core Startup: 这是我在ASP.NET Core Startup中添加DbContext服务的方法:

services.AddDbContextPool<AppDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

services.AddScoped<ClassA, ClassA>();
services.AddScoped<ClassB, ClassB>();

ClassA - Method1 ClassA-方法1

_dbContext.EntityX.Add(new EntityX { ... });

var product = ClassB.GetProduct(key);

_dbContext.EntityY.Add(new EntityY { Product = product });

_dbContext.SaveChanges();

ClassB - GetProduct B类-GetProduct

//Check product if it exists in the database
//Else, get details of the product from a Web API

var newProduct = new Product{ ... }
_dbContext.Products.Add(newProduct );

_dbContext.SaveChanges();

return newProduct;

I'll be honest and tell you I haven't tried running this yet, these codes are stripped (heavily) down version of the real code for brevity. 老实说,我还没有尝试过运行这些代码,为了简洁起见,这些代码被(大量)剥离了真实代码。

My questions are: 我的问题是:

1) Do I have the same DbContext in both ClassA and ClassB ? 1)我在ClassAClassB中都具有相同的DbContext吗? Is that guaranteed? 可以保证吗?

2) If both ClassA and ClassB have the same DbContext , will the SaveChanges in ClassB affects the EntityX I added in ClassA ? 2)如果ClassAClassB具有相同的DbContextClassBSaveChanges会影响我在ClassA添加的EntityX

3) What should I do to isolate changes in ClassA and ClassB ? 3)我应该怎么做才能隔离ClassAClassB更改?

Do I have the same DbContext in both ClassA and ClassB ? 我在ClassAClassB中都具有相同的DbContext吗? Is that guaranteed? 可以保证吗?

The database context are registered as scoped dependencies, that means that the instance is shared during the handling of a single incoming request. 数据库上下文被注册为作用域相关性,这意味着在处理单个传入请求期间共享实例。 Every request will get its own dependency injection scope, so within a single request, you are guaranteed to get the same database context instance. 每个请求将获得其自己的依赖项注入范围,因此,在单个请求内,可以确保获得相同的数据库上下文实例。

On the other hand, you are also guaranteed to get individual database contexts for separate requests. 另一方面,还可以确保为单独的请求获取单独的数据库上下文。

If both ClassA and ClassB have the same DbContext , will the SaveChanges in ClassB affect the EntityX I added in ClassA ? 如果ClassAClassB具有相同的DbContextClassBSaveChanges DbContext会影响我在ClassA添加的EntityX

The short answer to that is yes. 对此的简短答案是肯定的。 Since it's a single database context, there is also a single collection of entities that are being tracked. 由于它是单个数据库上下文,因此也有一个要跟踪的实体的单一集合。 So if you ran ClassA.Method1 first and did not have a SaveChanges call there, and then ran ClassB.Method2 , the SaveChanges() in the latter would also save the changes from the first method. 因此,如果您先运行ClassA.Method1并且没有在其中调用SaveChanges ,然后再运行ClassB.Method2 ,则后者中的SaveChanges()也将保存第一个方法的更改。

While this may sound problematic, in practice it usually is not: A request is typically handled as part of a controller action, so there is a very clear control flow. 虽然这听起来可能有问题,但实际上通常不是:请求通常作为控制器动作的一部分进行处理,因此控制流非常清晰。 As such, there isn't any parallel processing, and as long as you always “clean up” the context after using it (eg by calling SaveChanges() ) there will be no problem. 因此,没有任何并行处理,只要您在使用上下文后始终对其进行“清理”(例如,通过调用SaveChanges() ),就不会有问题。

On the opposite hand, you can also use this to your advantage and share transactions throughout the handling of your request without your methods knowing that they are part of a transaction. 相反,您也可以利用此优势,在整个请求处理过程中共享事务,而无需您的方法知道它们是事务的一部分。

What should I do to isolate changes in ClassA and ClassB ? 我应该怎么做才能隔离ClassAClassB更改?

Think about whether you actually need isolation. 考虑一下您是否真正需要隔离。 The only way your Method2 could impact Method1 is when Method1 performed changes to entities that are not saved. 你的唯一途径Method2可能会影响Method1是当Method1执行未保存修改的实体。 And if you are doing that, then you are likely misusing your entities anyway. 而且,如果您这样做了,那么您可能仍会滥用您的实体。 Usually, a method should complete its work on entities, without leaving a dirty state behind. 通常,方法应完成对实体的工作,而不会留下脏状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM