Dapper learning, dapper
Dapper has many extensions. I have used two types of Dapper, which are also the mainstream ones: Rainbow and Extension. Here I will introduce Rainbow first. After all, I will use it first. Of course, because I am using a mysql database (in the project, java and.. net, so mssql cannot be used, and mysql is used). Therefore, when using the Rainbow plug-in, you need to modify it. This is not for mysql.
Because of the time relationship, let's first introduce Create, the high-rise building from Create.
How to download Dapper. Rainbow from the project: PM> install-package dapper. rainbow
I. Create
After referencing the downloaded assembly, you can start encoding.
public class Rainbow : Database<Rainbow>{ public Table<Tch_Teacher> Teacher { get; set; } public Table<Tch_Contact> Contact { get; set; }}
The structure and name of the previous classes are not changed.
Test code:
var conStr = ConfigurationManager.ConnectionStrings["Cons"].ToString();using (var conn = new MySqlConnection(conStr)){ var db = Rainbow.Init(conn, 2000); try { db.BeginTransaction(); for (int i = 0; i < 10; i++) { var res = db.Teacher.Insert(new Tch_Teacher() { BId = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, IsDoublePosition = false, Name = "Haha" + i, No = i.ToString("000"), Sex = i % 2 }); } db.CommitTransaction(); } catch { db.RollbackTransaction(); Console.WriteLine("Error happened"); }}Console.ReadKey();
Result:
Ii. source code parsing
/// <Summary> /// Insert a row into the db // </summary> /// <param name = "data"> Either DynamicParameters or an anonymous type or concrete type </param> /// <returns> </returns> public virtual int Insert (dynamic data) {var o = (object) data; // obtain the field/attribute name List <string> paramNames = GetParamNames (o); // remove the Id, by default, each table has an auto-incrementing primary key Id paramNames. remove ("Id"); string cols = string. join (",", paramNames); // BId, CreateDate, IsDoublePosition, Name, No, Sex string cols_params = string. join (",", paramNames. select (p => "@" + p); // @ BId, @ CreateDate, @ IsDoublePosition, @ Name, @ No, @ Sex // var SQL = "set nocount on insert" + TableName + "(" + cols + ") values (" + cols_params + ") select cast (scope_identity () as int) "; original Rainbow statement // modify part of var SQL =" insert "+ TableName +" ("+ cols +") values ("+ cols_params + "); SELECT LAST_INSERT_ID () AS LastInsertedId "; return database. query <int> (SQL, o ). single ();}
When LastInsertedId is null, 0 is returned without affecting the result.
internal static List<string> GetParamNames(object o){ if (o is DynamicParameters) { return (o as DynamicParameters).ParameterNames.ToList(); } List<string> paramNames; if (!paramNameCache.TryGetValue(o.GetType(), out paramNames)) { paramNames = new List<string>(); foreach (var prop in o.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public)) { var attribs = prop.GetCustomAttributes(typeof(IgnorePropertyAttribute), true); var attr = attribs.FirstOrDefault() as IgnorePropertyAttribute; if (attr == null || (attr != null && !attr.Value)) { paramNames.Add(prop.Name); } } paramNameCache[o.GetType()] = paramNames; } return paramNames;}
From the code above, we can see that if you do not want to update some fields, you can add the feature [IgnoreProperty] to the field, and you can also see that the Insert parameter is a dynamic object, not necessarily Tch_Teacher, it can also be of another type, for example, in the new {BId = "111"} (frequently used) format.