Use of Dapper.columnmapper

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingnunit.framework;usingfluentassertions;namespacedapper.columnmapper.tests{[Testfixture] Public classcolumnmappertests {PrivateSqlConnection Connection;  Public Static ReadOnly stringConnectionString ="Data source=.\\sqlexpress;initial catalog=tempdb;integrated security=true";  Public classColumnmappingobject { Public stringDefaultcolumn {Get;Set; }  Public stringNonmatchingcolumn {Get;Set; } [ColumnMapping ("Mappedcol")]
Public stringMappedcolumn {Get;Set; } Public stringMiscasedcolumn {Get;Set; } } [Testfixturesetup] Public voidSetup () {connection=NewSqlConnection (connectionString); Connection. Open (); Createtesttable (); Sqlmapper.settypemap ( typeof(columnmappingobject), new columntypemapper (typeof (Columnmappingobject))); } Private voidcreatetesttable () {Const stringCreatesql =@"CREATE TABLE #Test (Id int, defaultcolumn varchar), badcol varchar, mappedcol varchar, misc Asedcolumn varchar) Insert #Test VALUES (1, ' DefaultColumn1 ', ' badcolumn ', ' MappedColumn1 ', ' Miscasedcol Umn1 ')"; Connection. Execute (Createsql); } [Testfixtureteardown] Public voidTearDown () {if(Connection! =NULL&& connection. state = =ConnectionState.Open) {Const stringDropsql ="drop table #Test"; Connection. Execute (Dropsql); Connection. Close (); } Connection=NULL; } [Test] Public voidproperty_matching_column_name_exactly_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.DefaultColumn.Should (). Notbenullorempty (); SelectedObject.DefaultColumn.Should (). Be ("DefaultColumn1"); } [Test] Public voidproperty_not_matching_coumn_name_exactly_and_no_attribute_is_not_mapped () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.NonMatchingColumn.Should (). Benullorempty (); } [Test] Public voidproperty_using_column_mapping_attribute_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.MappedColumn.Should (). Notbenullorempty (); SelectedObject.MappedColumn.Should (). Be ("MappedColumn1"); } [Test] Public voidmiscased_property_is_mapped_correctly () {varSelectedobject =selectobjects (); Selectedobject.should (). Notbenull (); SelectedObject.MiscasedColumn.Should (). Notbenullorempty (); SelectedObject.MiscasedColumn.Should (). Be ("MiscasedColumn1"); } Privatecolumnmappingobject selectobjects () {Const stringSelectsql ="SELECT * from #Test"; returnConnection. Query<columnmappingobject>(Selectsql). FirstOrDefault (); } [Test] Public voidCan_register_single_type () {columntypemapper.registerfortypes (typeof(DateTime)); varMap = Sqlmapper.gettypemap (typeof(DateTime)); Map. Should (). Notbenull (); Map. Should (). Beoftype<ColumnTypeMapper>(); } [Test] Public voidcan_register_multiple_types () {columntypemapper.registerfortypes (typeof(DateTime),typeof(TimeSpan)); varDatetimemap = Sqlmapper.gettypemap (typeof(DateTime)); Datetimemap.should (). Notbenull (); Datetimemap.should (). Beoftype<ColumnTypeMapper>(); varTimespanmap = Sqlmapper.gettypemap (typeof(TimeSpan)); Timespanmap.should (). Notbenull (); Timespanmap.should (). Beoftype<ColumnTypeMapper>(); } }}

Use of Dapper.columnmapper

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.