In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "what is the plug-in that makes Dapper more powerful in C#". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the plug-in that makes Dapper stronger in C#"?
1. Dapper Contrib
Dapper Contrib extends Dapper's CRUD method for entity classes:
Installation method:
Command line:
Dotnet add package Dapper.Contrib
NuGet:
Install-Package Dapper.Contrib
Use:
Using Dapper.Contrib.Extensions
This is an extension package that makes Dapper more powerful. Because CRUD is supported, you need to add configuration to the entity class, which uses Attribute as the basis for related mapping configuration:
[Table ("Model")]
Public class Model
{
[Key]
[ExplicitKey]
Public int Id {get;set;}
[Computed]
Public int Count {get;set;}
[Write]
Public String Name {get;set;}
}
This is all the configuration. Table is used to declare a table and the table name must be specified. Key indicates that the attribute is the primary key of the database, ExplicitKey indicates that this property is the primary key of the display setting in the database, Computed indicates that the field is a calculated field, and Write indicates that the field can be set into it. It is important to note that Key and ExplicitKey cannot be marked on the same attribute at the same time.
So next, let's take a look at what methods it extends:
Insert a single object:
Public static long Insert (this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
Where transcation represents a transaction, and if a transaction is specified, the commit of the data will be controlled by the transaction, and this method returns the primary key of the inserted object (if the primary key of the object is of numeric type) or the number of rows that have been inserted in a list to be inserted.
Get a single object:
Public static T Get (this IDbConnection connection, [Dynamic] dynamic id, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
Get a data by passing in the primary key
Get all the data:
Public static IEnumerable GetAll (this IDbConnection connection, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
Update data:
Dapper Contrib provides a method for updating:
Public static bool Update (this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
What's interesting about this method is that
Var entity = connection.Get (1)
Entity.Name = "Test 1"
Connection.Update (entity)
And
Var models = connection.GetAll ()
Foreach (var m in models)
{
Console.WriteLine (m)
M.StringLength + +
}
Connection.Update (models.AsList ())
It's all right, and it won't make a mistake.
It is important to note, however, that if the instance that needs to be updated does not specify a primary key value (the primary key property is not assigned), no rows will be updated. And when you update, all columns are updated, not just because you don't assign a value.
There are two ways to delete:
Public static bool Delete (this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
Public static bool DeleteAll (this IDbConnection connection, IDbTransaction transaction = null, int? CommandTimeout = null) where T: class
Deletion also passes in an entity class, which only requires a value for the primary key. If the data corresponding to the primary key is not found, there will be no change. Delete, like Update, is OK if you pass in a List collection.
2. Dapper Transaction
This package extends the transaction processing capabilities of Dapper. Although it is an extension package for Dapper, it adds an extension method to IConnection. Examples of use are as follows:
Dotnet add package Dapper.Transaction
The old rule, remember to add the bag first.
Then the code goes like this:
Using Dapper.Transaction
Using (var connection = new SqliteConnection ("Data Source=./demo.db"))
{
Connection.Open ()
Var transcation = connection.BeginTransaction ()
/ / write business code
Transcation.Commit ()
}
If you use Dapper Transaction, you need to call connection.Open () first to ensure that the connection is open.
Transcation can be used as a normal DbTranscation object, as a method passed to Dapper, or as a Dapper client that opens a transaction. That is, Dapper extends the method to IDbConnection, and in this package it also extends the method to respond to IDbTranscation:
3. Dapper Plus
This plug-in is a plug-in for dealing with huge amounts of data on Dapper, but it is a paid version of the plug-in, but there is a certain trial period every month. If you want to try it, you can do the following:
Dotnet add package Z.Dapper.Plus
Use:
Using Z.Dapper.Plus
Before using this plug-in, you need to configure the mapping between the entity class and the database:
DapperPlusManager.Entity () .Table (Customers)
DapperPlusManager.Entity () .Table ("Suppliers") .identity (x = > x.SupplierID)
The plug-in supports four sets of mass processing methods:
Bulk Insert
Bulk Update
Bulk Merge
Bulk Delete
/ / STEP MAPPING
DapperPlusManager.Entity () .Table ("Suppliers") .identity (x = > x.SupplierID)
DapperPlusManager.Entity () .Table ("Products") .identity (x = > x.ProductID)
/ / STEP BULKINSERT
Using (var connection = new SqlConnection (FiddleHelper.GetConnectionStringSqlServerW3Schools ()
{
Connection.BulkInsert (suppliers) .ThenForEach (x = > x.Products.ForEach (y = > y.SupplierID = x.SupplierID)) .ThenBulkInsert (x = > x.Products)
}
/ / STEP BULKUPDATE
Using (var connection = new SqlConnection (FiddleHelper.GetConnectionStringSqlServerW3Schools ()
{
Connection.BulkUpdate (suppliers, x = > x.Products)
}
/ / STEP BULKMERGE
Using (var connection = new SqlConnection (FiddleHelper.GetConnectionStringSqlServerW3Schools ()
{
Connection.BulkMerge (suppliers) .ThenForEach (x = > x.Products.ForEach (y = > y.SupplierID = x.SupplierID)) .ThenBulkMerge (x = > x.Products)
}
/ / STEP BULKDELETE
Using (var connection = new SqlConnection (FiddleHelper.GetConnectionStringSqlServerW3Schools ()
{
Connection.BulkDelete (suppliers.SelectMany (x = > x.Products)) .BulkDelete (suppliers)
} at this point, I believe you have a deeper understanding of "what is the plug-in that makes Dapper stronger in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.