Dapper custom database fields and code mapping method when the model field is inconsistent

Source: Internet
Author: User

namespaceyournamespace{/// <summary>    ///Uses The Name value of the Columnattribute specified, otherwise maps as usual. /// </summary>    /// <typeparam name= "T" >The type of the object is this mapper applies to.</typeparam>     Public classColumnattributetypemapper<t>: fallbacktypemapper { Publiccolumnattributetypemapper ():Base(Newsqlmapper.itypemap[] {NewCustompropertytypemap (typeof(T), (type, columnName)=type. GetProperties (). FirstOrDefault (Prop=Prop. GetCustomAttributes (false)                                   . OfType<ColumnMappingAttribute>()                                   . Any (attr= = attr. Name = =columnName)) ),                    NewDefaulttypemap (typeof(T))}) {}} [AttributeUsage (Attributetargets.property, AllowMultiple=true)]     Public classColumnmappingattribute:attribute { Public stringName {Get;Set; } }    Public classFallbackTypeMapper:SqlMapper.ITypeMap {Private ReadOnlyIenumerable<sqlmapper.itypemap>_mappers;  PublicFallbacktypemapper (ienumerable<sqlmapper.itypemap>mappers) {_mappers=mappers; }           PublicConstructorInfo Findconstructor (string[] names, type[] types) {            foreach(varMapperinch_mappers) {                Try{constructorinfo result=Mapper.                    Findconstructor (names, types); if(Result! =NULL)                    {                        returnresult; }                }                Catch(notimplementedexception) {}}return NULL; }          PublicSqlmapper.imembermap Getconstructorparameter (ConstructorInfo constructor,stringcolumnName) {            foreach(varMapperinch_mappers) {                Try                {                    varresult =Mapper.                    Getconstructorparameter (constructor, ColumnName); if(Result! =NULL)                    {                        returnresult; }                }                Catch(notimplementedexception) {}}return NULL; }          PublicSqlmapper.imembermap GetMember (stringcolumnName) {            foreach(varMapperinch_mappers) {                Try                {                    varresult =Mapper.                    GetMember (ColumnName); if(Result! =NULL)                    {                        returnresult; }                }                Catch(notimplementedexception) {}}return NULL; }    }  }
 Public Static IEnumerable loadwithcustommapping () {    using (var conn = opendbconnection ())    {        Dapper.SqlMapper.SetTypeMap (typeofnew  columnattributetypemapper ());         return Conn. Query ("select TOP caid,caname,en from Cityarea");}    }

Dapper custom database fields and code mapping method when the model field is inconsistent

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.