Dapper simplecrud, dappersimplecrud
Dapper extensions Dapper simplecrud can be used to facilitate dapper operations.
1. Click Manage NuGet first
2. Search Dapper. SimpleCRUD on the Management page and install
Then you can use the extension under this package.
After searching for this article for a long time, I casually spoke about Baidu, which is really very boring and found too much useless information.
Https://github.com/ericdc1/Dapper.SimpleCRUD/
Summarize the methods to facilitate future search
The following is a summary of the use of the method
1. Get Method
public static T Get<T>(this IDbConnection connection, int id)
First, create the object class corresponding to the table
public class User{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}
Then query the object
var user = connection.Get<User>(1);
Equivalent to SQL:
Select Id, Name, Age from [User] where Id = 1
Next we will make some changes to the object class.
[Table ("Users")] // real Table name public class User {[Key] public int UserId {get; set ;} [Column ("strFirstName"] // real Column name public string FirstName {get; set;} // Column alias public string LastName {get; set;} public int Age {get; set ;}} var user = connection. get <User> (1 );
After modification, the query is equivalent to SQL
Select UserId, strFirstName as FirstName, LastName, Age from [Users] where UserId = @UserID
2. GetList Method
public static IEnumerable<T> GetList<T>(this IDbConnection connection)
Entity:
public class User{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}
Query all
var user = connection.GetList<User>();
Equivalent to SQL
Select * from [User]
Use conditional Object Query
public class User{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}var user = connection.GetList<User>(new { Age = 10 });
Equivalent to SQL
Select * from [User] where Age = @Age
Query using string Conditions
public class User{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}var user = connection.GetList<User>("where age = 10 or Name like '%Smith%'");
Equivalent to SQL
Select * from [User] where age = 10 or Name like '%Smith%'
Paging query:
public static IEnumerable<T> GetListPaged<T>(this IDbConnection connection, int pageNumber, int rowsPerPage, string conditions, string orderby)
public class User{ public int Id { get; set; } public string Name { get; set; } public int Age { get; set; }}var user = connection.GetListPaged<User>(1,10,"where age = 10 or Name like '%Smith%'","Name desc");
Equivalent to SQL: SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY Name desc) AS PagedNumber, Id, Name, age FROM [User] where age = 10 or Name like '% Smith %') AS u WHERE PagedNUMBER BETWEEN (1-1) * 10 + 1) AND (1*10)
Insert Method
Public static int Insert (this IDbConnection connection, object entityToInsert)
[Table ("Users")] public class User {[Key] public int UserId {get; set;} public string FirstName {get; set;} public string LastName {get; set;} public int Age {get; set;} // Additional properties not in database [Editable (false)] public string FullName {get {return string. format ("{0} {1}", FirstName, LastName) ;}} public List <User> Friends {get; set ;} [ReadOnly (true)] public DateTime CreatedDate {get; set;} var newId = connection. insert (new User {FirstName = "User", LastName = "Person", Age = 10 });
Equivalent to SQL
Insert into [Users] (FirstName, LastName, Age) VALUES (@FirstName, @LastName, @Age)
Update Method
[Table ("Users")] public class User {[Key] public int UserId {get; set;} [Column ("strFirstName")] public string FirstName {get; set;} public string LastName {get; set;} public int Age {get; set;} // Additional properties not in database [Editable (false)] public string FullName {get {return string. format ("{0} {1}", FirstName, LastName) ;}} public List <User> Friends {get; set ;}} connection. update (entity );
Equivalent to SQL
Update [Users] Set (strFirstName=@FirstName, LastName=@LastName, Age=@Age) Where ID = @ID
Delete Method
Public static int Delete <T> (this IDbConnection connection, int Id) public class User {public int Id {get; set;} public string FirstName {get; set ;} public string LastName {get; set;} public int Age {get; set ;}} connection. delete <User> (newid); or public static int Delete <T> (this IDbConnection connection, T entityToDelete) public class User {public int Id {get; set ;} public string FirstName {get; set;} public string LastName {get; set;} public int Age {get; set ;}} connection. delete (entity );
Equivalent to SQL
Delete From [User] Where ID = @ID
Delete multiple
1. Delete public static int DeleteList <T> (this IDbConnection connection, object whereConditions, IDbTransaction transaction = null, int? CommandTimeout = null) connection. deleteList <User> (new {Age = 10}); 2. delete public static int RecordCount <T> (this IDbConnection connection, string conditions = "") connection. deleteList <User> ("Where age> 20 ");
Statistics
public static int RecordCount<T>(this IDbConnection connection, string conditions = "")var count = connection.RecordCount<User>("Where age > 20");