91手机视频在线-91手机视频在线观看-91手机在线-91手机在线播放-91手机在线观看

完善主體資料,免費贈送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

主站蜘蛛池模板: 91精品免费久久久久久久久 | 国产精品久久久久久久久久久不卡 | 全部毛片 | 黄色网免费 | 农村高清性色生活片 | 亚洲第一页国产 | 1级a的观看视频 | 无码精品一区二区三区免费视频 | va在线| 色999| 特一级黄色 | 国产精品v欧美精品v日韩 | 在线爱爱视频 | 欧美亚洲日本国产综合网 | 一级成人a毛片免费播放 | 免费国产人做人视频在线观看 | 久久久黄色大片 | 亚洲jjzzjjzz在线播放 | 91在线播放免费不卡无毒 | 国产精品视频一区麻豆 | 亚洲国产欧美国产第一区 | 欧美一级大黄 | 久久青青草原国产精品免费 | 91视频免费观看高清观看完整 | 国产精品永久免费自在线观看 | 国产二级毛片 | 国产高清视频免费在线观看 | 成人 在线欧美亚洲 | 亚洲国产精品va在线观看麻豆 | 最新国产中文字幕 | 色在线视频 | 伊人久久中文字幕 | 中美日韩在线网免费毛片视频 | 99精品视频在线观看免费播放 | chinese调教在线播放 | 亚洲国产精品网站久久 | 夜夜夜精品视频免费 | 黄色影院视频 | 精品亚洲综合在线第一区 | 高清在线观看自拍视频 | 日韩美女中文字幕 |