In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, the editor will bring you about the way EntityFramework Core is separated from reading and writing. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
What is a transaction? We can boil it down to one sentence: multiple submissions are either all successful or all fail, that is, live and die together, and there are no escapees.
So the question is, what are the uses or advantages of using transactions? Transactions allow us to package combinations of related operations to ensure consistency of application data.
So what are the disadvantages of using transactions? Although using transactions ensures data consistency, and so on, it can affect performance and may cause deadlocks.
So the question comes again, since there are advantages and disadvantages, can we achieve data consistency with handwritten logic? Of course, we can simulate the effect of transaction rollback and commit, but this is not 100% guaranteed.
Is the call to the SaveChanges method in a transaction?
First, we add the following data in the console, and then add log printing.
We know by printing the log that the commit is included in the transaction when the SaveChanges method is called, so please add the following open transaction to those who can use multiple tables in the project to worry about problems, which is obviously superfluous.
Seeing that the log information above is not more certain, let's take a look at the context.Database.AutoTransactionsEnabled method in context, which is explained in detail as follows:
We know from the interpretation of the AutoTransactionsEnabled method that its default value is True, which means that transactional commit will be used when the SaveChanges method is called. Of course, we can set whether to disable transactions globally in the context constructor, as follows:
When will we use transactions in EF Core? If it is a single context, a single database, then the transaction has nothing to do with us, we don't have to worry about the transaction at all. If you are using multiple contexts (across contexts) or multiple databases in a single database, the transaction will shine.
For example, for goods, shopping carts, order management, payment and logistics in e-commerce, we can instantiate five different contexts, which will involve using transactions to maintain data consistency in the following operations. of course, this is in the same relational database. Or instantiate the same context multiple times to use transactions to maintain data consistency.
You can refer to the introduction "https://docs.microsoft.com/en-us/ef/core/saving/transactions" on the official website. It's all about the same database operation, nothing more than what I call cross-context, using context combined with underlying DbConnection to use transaction sharing connections, and so on.
A slightly larger point is the introduction of System.Transactions in EF Core 2.1to specify the isolation level as well as the use of ambient transactions (lookup data is used for multiple transactions, there are connections between transactions, so the entire scope will appear very lengthy, by using this transaction, all connections will be included in the transaction), there is no room for introduction here.
Like everyone else, we are most concerned with distributed transactions, that is, using different contexts for multiple databases, but unfortunately distributed transactions are not supported until EF Core 2.1, because the relevant APi in the .NET Core is not perfect, so keep waiting.
With the entry of traffic, the database will be subject to irresistible pressure, and a single database will no longer be applicable. This is all due to iterative changes in the architecture brought about by the evolution of the project, which involves sub-database. For queried data as a separate database, as data changes are also used as a separate database, combined with those load balancing and so on, the database pressure will be reduced a lot.
The database that only makes the query is called the slave database. For the database changed by the database, it is called the master database, and there are many ways to synchronize the Master-Slave data.
Although I haven't had any contact with it, it's okay to use replication in SQL Server for publish-subscribe to simulate the demo. Let's take a look at how .NET Core Web applications achieve read-write separation.
In addition, I have not used it in the project. It is all my private research. Whether the plan is feasible or not can be discussed together. We created two Demo databases, as follows:
We use Demo1 as the master database and Demo2 as the slave database, and then use a dynamic diagram to demonstrate the creation of replication publish-subscriptions (published every 10 seconds).
We give the Demo1 context, and Demo2 is the same as it is, and next we should inject the Demo1 and Demo2 context into the .NET Core Web application as follows:
Then we create the Demo controller, add the data through the Demo1 context, and read the data in the Demo2 context, as follows:
We see that after adding data through the Demo1 context, we are redirected to the list page queried by the Demo2 context, and automatically synchronize to the Demo2 database in 10 seconds, and you can see the data display through refresh.
Although the result is as we expected, the implementation path does not make us so satisfactory, because the entities used are all the same, just that the connected database is different, but we need to create two different context instances. obviously this is not the best practice.
So what are our best practices? Next, let's create a Demo3 database with the same table structure as Demo1 and Demo2, as follows:
Next, we create the following extension method in the class library where the Demo1 and Demo2 context of the .NET Core Web application is located (for the convenience of peers to learn, give the basic structure of the Demo project).
Let's not look at why this is set up for the moment, we just add a context extension method to change the database connected to Demo3, and then when we get the list of blogs, we call the above extension method. Can we get the Demo3 data or throw an exception? We still demonstrate it through a dynamic graph, as follows:
We have always thought that we can use the context.Database.GetDbConnection () method to go back to ADO.NET for query.
But we have proved that we can set up other database connections to achieve read-write separation best practices without instantiating another context.
Therefore, for the Demo1 and Demo2 contexts we configured above, we can only need the Demo1 context as the master database. For querying from the slave database, we just need to change the connection string based on the Demo1 context, as follows:
Then the question arises, so why change the Demo1 context connection string to transfer to another database query? This is to solve the problem of read-write separation without instantiating context, that is, Demo2, but how is it implemented internally?
Because a method is added inside EF Core to implement the IRelationalConnection interface, we can reset the connection string on the existing context instance, that is, change the database, but only if the current context connection is closed.
That is to say, for example, we use the current context to change the operation in the same transaction, then change the connection string to make the change operation, and finally commit the transaction, because within this transaction, the current context connection has not been closed, so the database change operation after changing the connection string is bound to throw an exception.
This is how the editor separates the reading and writing of the EntityFramework Core shared by you. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.