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

完善主體資料,免費(fèi)贈(zèng)送VIP會(huì)員!
* 主體類型
* 企業(yè)名稱
* 信用代碼
* 所在行業(yè)
* 企業(yè)規(guī)模
* 所在職位
* 姓名
* 所在行業(yè)
* 學(xué)歷
* 工作性質(zhì)
請(qǐng)先選擇行業(yè)
您還可以選擇以下福利:
行業(yè)福利,領(lǐng)完即止!

下載app免費(fèi)領(lǐng)取會(huì)員

NULL

ad.jpg

二次開(kāi)發(fā)教程:Dapper里使用Attribute自定義映射關(guān)系

發(fā)布于:2019-07-24 16:32:10

網(wǎng)友投稿

更多

目的將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>());//定義映射規(guī)則

            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; }

    }

本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。

未標(biāo)題-1.jpg

上一篇:二次開(kāi)發(fā)教程:Hello entity framework

下一篇:二次開(kāi)發(fā)教程:orm 里使用Emit

主站蜘蛛池模板: 桃花福利视频在线观看 | 日韩精品一区二区三区免费观看 | 任你敢不一样精品的视频 | 97r久久精品国产99国产精 | 五月婷婷六月丁香 | 天天色踪合 | 亚洲系列_1页_mmyy11 | 亚洲另类在线观看 | 国内精品久久久久久影院老狼 | 久草看片| 性感一级毛片 | 日韩经典欧美精品一区 | 一道本色 | 亚洲精品国产美女在线观看 | 久久国产一久久高清 | 国产不卡视频在线 | 国产精品七七在线播放 | 国产成人精品免费视 | 久久青草免费91线频观看站街 | 网站久久 | 日韩欧美一区二区三区视频 | 国产成人精品免费午夜 | 欧美激情成人网 | 亚洲激情在线 | 晚上睡不着偷偷看b站免费是视频 | 国产成人精品微拍视频 | 激情毛片视频在线播放 | 一级黄色片一 | 97青青草原国产免费观看 | 成人免费淫片在线费观看 | 国产亚洲欧美在线 | 亚洲伦理一区二区 | 国产 日韩 欧美 高清 | 日韩在线观看精品 | 中文字幕伊人久久网 | 久久免费精品国产72精品剧情 | 一区二区不卡免费视频 | 丁香婷婷综合网 | 全黄性色大片 | 国产成人剧情 | 亚洲欧美日韩高清一区二区一 |