EntityFramework-based database master-slave read/write separation service plug-in 1. Version information and source code
1.1 Version Information
v1.0 Beta (2015-04-02), developed based on EF 6.1, supports all EF6 versions after EF 6.1.
1.2 Open Source Address
Https://github.com/cjw0511/NDF.Infrastructure
About the EF database master-slave read/write separation service core source is located in the folder:
Src\ NDF. The Data.entityframework\masterslaves folder.
2. Overview of functions
2.1 support for data manipulation based on EF6:
2.1.1 automatically forwards requests to the primary server (master, or write operations server) for all data write operations;
2.1.2 automatically forwards requests to the slave server (Slave, the Query Operations server) for all data query operations;
2.1.3 The database operation request forwarding is done by changing the database connection string before executing the command, but the change action of the data connection string does not require the business developer to alter any existing code;
2.2 supports a master multi-slave management when forwarding a read-write command request to the appropriate database server
That is, you can set up a database server as Master server, and can set up one or more database servers as Slave server;
Note: There is a need to establish a data synchronization mechanism between the Master server and the Slave server, which can be done by configuring the DBMS system.
2.3 support automatic detection of server running status:
2.3.1 can automatically detect the online status of Master server;
2.3.2 automatically detects the online status of each Slave server node in the Set Slave server list;
2.3.3 can be customized to automatically detect the state of the server time frequency;
2.4 support to automatically switch to the Master node when the Slave server node is unavailable:
If more than one Slave server node is set, the available server nodes will be automatically selected for each query operation, based on the Slave server online status of automatic detection, and if all Slave are unavailable, the data query operation can be automatically switched to Master server based on configuration;
2.5 support to automatically switch to the Slave node when the Master server node is unavailable:
When the EF6-based data change operation is detected, if the Master server state is not available, you can determine, based on the configuration, whether to automatically switch the data change operation to the first available item in the Slave server list (this setting is generally not recommended because the Slave server is the Master Although the use of the server can make the application not offline after Master failure, it also leads to data consistency issues between the Slave server nodes. );
2.6 support for load balancing between multiple Slave nodes:
If more than one Slave server node is set, each time a query operation is performed, the first available Slave server is selected in the set order, and any of the available Slave servers can be randomly selected (this setting effectively disperses the Slave Server query pressure) to execute the query command.
2.7 support for multi-DbContext type configurations in EF:
If you use more than one type of EF entity context (System.Data.Entity.DbContext) object in your project, it is supported to configure different master-slave read-write separate database connection schemes for each different type of DbContext.
2.8 supports hot-swappable configuration of Master server nodes and Slave server nodes:
That can not stop the operation of the project, directly by modifying the contents of the configuration file Ef.masterslave.config, to achieve automatic refresh related configuration connection effect;
3. Instructions for use
3.1 to set up automatic synchronization between multiple database server instances
It is preferred to configure the master-slave automatic synchronization mechanism between multiple database server instances through a database management system (DBMS), such as:
3.1.1 If the MSSQLSERVER database system is used, the replication and subscription policies between multiple database server instances can be configured.
3.1.2 If you are using MySQL database system, you can configure the master-slave replication policy between multiple database server instances;
3.1.3 Other Oracle, DB2 ...
3.2 to add a configuration file to a project
Add the configuration file Ef.masterslave.config under the project root and modify the contents according to the rules, here is a reference to configure the way:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <configuration> 3 <configSections> 4 <section Name = "Ef.masterslave" type= "NDF. Data.EntityFramework.MasterSlaves.ConfigFile.EFMasterSlaveSection, NDF. Data.entityframework "5 requirepermission=" false "/> 6 </configSections> 7 <ef.masterslave> ; 8 <!--The following is the configuration of master-slave read-write separation service for all database operations that use the EF entity context (DbContext) type MyProject.Data.MyDbContext-9 <applyitem Targe Tcontext= "MyProject.Data.MyDbContext, Myproject.data" autoswitchslaveonmasterfauled= "false" Autoswitchmas Teronslavesfauled= "true" slaverandomization= "true" slavescaninterval= ">12 <master Connect" ionstring= "Server=192.168.0.99;port=3306;user Id=root;password=123456;persistsecurityinfo=true;database=testdb; Convertzerodatetime=true;allowzerodatetime=true "/>13 <slaves>14 <add connectionString=" server=1 92.168.0.101;port=3306;user id=root;password=123456;persIstsecurityinfo=true;database=testdb;convertzerodatetime=true;allowzerodatetime=true "order=" 0 "/>15 <add Co nnectionstring= "Server=192.168.0.102;port=3306;user id=root;password=123456;persistsecurityinfo=true;database= Testdb;convertzerodatetime=true;allowzerodatetime=true "order=" 1 "/>16 <add connectionstring=" server=192.168 .0.103;port=3306;user Id=root;password=123456;persistsecurityinfo=true;database=testdb;convertzerodatetime=true ; Allowzerodatetime=true "order=" 2 "/>17 </slaves>18 </applyitem>19 <!--The following is for another EF entity context (D Bcontext) Configure master-slave read/write separation service-->20 <!--<applyitem ... = "" >21 <master ... = "/>22 <slaves>23 <add = "/>24 <add ... =" "/>25 </slaves>26 </applyitem>-->27 &L T;/ef.masterslave>28 </configuration>
3.3 to introduce a dependent package in a project
3.3.1 EntityFramework 6.1 or higher;
3.3.2 Microsoft Enterprise library-data Access application Block 6;
3.3.3 Newtonsoft.Json.dll 6.0 or higher;
3.3.4 NDF. Utilities.dll;
3.3.5 NDF. Data.dll;
3.3.6 NDF. Data.EntityFramework.dll;
3.4 to add a startup code to your project
In the startup code of the project (console and desktop programs are typically the Main method for the program type, and the ASP. Application_Start code block for the Global.asax file), add the following snippet:
1 NDF. Data.EntityFramework.MasterSlaves.EFMasterSlaveConfig.Register (typeof (Mydbcontext));
The type parameter passed in the method should be the type shown in the Targetcontext property of the Applyitem section in the Ef.masterslave.config configuration file, indicating which type of EF entity context (DBCONTEXT) to configure for the read-write separation service.
4. Other
4.1 The automatic synchronization mechanism of related data content in master-slave database is completed by database management system (DBMS, such as MSSQLSERVER, Oracle, MySQL, DB2, etc.), and the function of this part is not provided by this plugin; almost all mainstream DBMS The system provides the function of automatic synchronization mechanism of master-slave database;
4.2 The EF database master-Slave read-write separation scheme supports all common database transactions and distributed transaction operations, although distributed transactions also require the support of the database management system (DBMS) otherwise invalid;
4.3 In the database master-slave read-write separation operation based on the EF6 and the plug-in, the program automatically detects the transaction state of the database operations performed and automatically forwards all add-ons and query requests with database transactions or distributed transactions to the Master server.
4.4 This article is just an overview of the introduction of I wrote this EF database master-Slave read-write separation plug-in, about the plug-in implementation of the principle of the source code and ideas, I will be in the post in the future to expand the introduction.
Database Master-Slave read-write separation service plug-in based on EntityFramework