完善主體資料,免費贈送VIP會員!
      * 主體類型
      * 企業名稱
      * 信用代碼
      * 所在行業
      * 企業規模
      * 所在職位
      * 姓名
      * 所在行業
      * 學歷
      * 工作性質
      請先選擇行業
      您還可以選擇以下福利:
      行業福利,領完即止!

      下載app免費領取會員

      NULL

      ad.jpg

      二次開發教程:Dapper里使用Attribute自定義映射關系

      發布于:2019-07-24 16:32:10

      網友投稿

      更多

      目的將book表中的id,name,price 映射到Book類中的Id1,Name1,Price1


          class Program

          {        

              static SQLiteConnection conn;

              static string dbStr = "test.db";

              static void Main(string[] args)

              {

                  SqlMapper.SetTypeMap(typeof(Book), new CustomTypeMap<Book>());//定義映射規則

                  SQLiteConnection.CreateFile(dbStr);

                  conn = new SQLiteConnection($"Data Source={dbStr};Version=3;");

                  string sql = "create table book (id int,name varchar(20), price double)";         

       

                  conn.Execute(sql);

                  sql = "insert into book values(@id1,@name1,@price1)";

                  conn.Execute(sql, new Book());

       

                  sql = "select * from book";

                  var books = conn.Query<Book>(sql);

       

                  Console.ReadLine();

              }

          }

       

          public class Book

          {

              [Column("name")]

              public string Name1 { get; set; }

              [Column("id")]

              public int Id1 { get; set; }

              [Column("price")]

              public double Price1 { get; set; }

          }

       

          public class CustomTypeMap<T> : SqlMapper.ITypeMap

          {

              public ConstructorInfo FindConstructor(string[] names, Type[] types)

              {

                  return null;

              }

       

              public ConstructorInfo FindExplicitConstructor()

              {

                  return typeof(Book).GetConstructor(new Type[0]);

              }

       

              public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)

              {

                  return null;

              }

       

              public SqlMapper.IMemberMap GetMember(string columnName)

              {

                  return new CustomMemberMap(columnName, _dict[columnName]);

              }

       

              private Dictionary<string, PropertyInfo> _dict = new Dictionary<string, PropertyInfo>();

              public CustomTypeMap()

              {

                  Type type = typeof(T);

                  var ps = type.GetProperties();

                  foreach (var p in ps)

                  {

                      var at = p.GetCustomAttribute<ColumnAttribute>();

                      if (at != null)

                      {

                          if (!string.IsNullOrWhiteSpace(at.ColumnName))

                              _dict.Add(at.ColumnName, p);

                      }

                  }

              }

          }

          [AttributeUsage(AttributeTargets.Property)]

          public class ColumnAttribute : Attribute

          {

              public string ColumnName { get; }

              public ColumnAttribute(string columnName)

              {

                  ColumnName = columnName;

              }

          }

       

          public class CustomMemberMap : SqlMapper.IMemberMap

          {

              public CustomMemberMap(string column,PropertyInfo propertyInfo)

              {

                  ColumnName = column;

                  Property = propertyInfo;

              }

              public string ColumnName { get; }

       

              public Type MemberType => Field?.FieldType ?? Property?.PropertyType ?? Parameter?.ParameterType;

       

              public PropertyInfo Property { get; }

       

              public FieldInfo Field { get; }

       

              public ParameterInfo Parameter { get; }

          }

      本文版權歸腿腿教學網及原創作者所有,未經授權,謝絕轉載。

      未標題-1.jpg

      上一篇:二次開發教程:Hello entity framework

      下一篇:二次開發教程:orm 里使用Emit

      主站蜘蛛池模板: 88国产精品视频一区二区三区| 国产精品免费综合一区视频| 亚洲av乱码一区二区三区| 一本AV高清一区二区三区| 亚洲中文字幕丝袜制服一区 | 成人无码AV一区二区| 国产日本一区二区三区| 冲田杏梨高清无一区二区| 久久亚洲色一区二区三区| 精品一区二区ww| 无码人妻AV免费一区二区三区| 一区二区三区国模大胆| 冲田杏梨AV一区二区三区| 久久婷婷色综合一区二区| 亚洲国产成人久久一区久久 | 国产精品夜色一区二区三区| 国产一区二区在线观看视频| 亚洲AV日韩综合一区| 亚洲国产成人久久一区WWW| 精品无码成人片一区二区98| 秋霞无码一区二区| 亚洲日本乱码一区二区在线二产线| 亚洲AV无码一区二区二三区入口| 中文字幕精品无码一区二区| 一级特黄性色生活片一区二区| 性色AV 一区二区三区| 亚洲熟妇av一区二区三区漫画| 一本大道东京热无码一区 | 亚洲乱码日产一区三区 | 综合久久一区二区三区 | 久久青草精品一区二区三区| 久久国产精品亚洲一区二区| 免费无码VA一区二区三区| 国产精品无码亚洲一区二区三区| 国产精品视频一区二区三区经| 久久影院亚洲一区| 三上悠亚亚洲一区高清| 一区二区中文字幕在线观看| 无码欧精品亚洲日韩一区| 免费无码AV一区二区| 国产精品一区二区久久不卡|