Dapper.Bulk contains helper methods for bulk inserting.
PM> Install-Package Dapper.Bulk
- Inserts entities, without result for best performance:
connection.BulkInsert(data);awaitconnection.BulkInsertAsync(data);- Inserts and returns inserted entities:
varinserted=connection.BulkInsertAndSelect(data);varinserted=awaitconnection.BulkInsertAndSelectAsync(data);TableNameis TypeName + s. When InterfaceIis removed.Keyis Id property (case-insensitive)
TableName - somewhere before usage call.
TableMapper.SetupConvention("tbl","s")We do not rely on specific attributes. This means you can use whatever attributes with following names:
TableAttribute- Must have string Name property. Exists in System.ComponentModel.Annotations Nuget.ColumnAttribute- Must have string Name property. Exists in System.ComponentModel.Annotations Nuget.KeyAttribute- Marking only attribute. Exists in System.ComponentModel.Annotations Nuget.ComputedAttribute- Marking only attribute. For fields returned from Db.NotMapped- Marking only attribute. For ignored fields.
// Table Cars by default convention publicclassCar{// Identity by conventionpublicintId{get;set;}publicstringName{get;set;}publicDateTimeManufactureDate{get;set;}}// Supported in v1.2+publicenumCarType:int{Classic=1,Coupe=2}[Table("tblCars")]publicclassCar{[Key]// IdentitypublicintCarId{get;set;}publicstringName{get;set;}publicCarTypeCarType{get;set;}//SQL Data Type should match Enum type[Computed]// Will be ignored for inserts, but the value in database after insert will be returnedpublicDateTimeManufactureDate{get;set;}}publicclassIdentityAndNotMappedTest{[Key]publicintIdKey{get;set;}publicstringName{get;set;}// Will be ignored for insertspublicvirtualTestSublassTestSublass{get;set;}[NotMapped]// Will be ignored for insertspublicintIgnored{get;set;}}// Supported in v1.4+privateclassCustomColumnName{[Key]publicintIdKey{get;set;}[Column("Name_1")]// Will map to SQL column Name_1publicstringName{get;set;}[Column("Int_Col")]// Will map to SQL column Int_ColpublicintIntCol{get;set;}[Column("Long_Col")]// Will map to SQL column Long_ColpubliclongLongCol{get;set;}[NotMapped]// Will be ignored for insertspublicintIgnored{get;set;}[Write(false)]// Will be ignored for insertspublicintIgnored{get;set;}}