Public BOOLCreatetb_property_modellbylist (list<tb_property_model>entity) { Try { if(Entity. Count >0) { stringstrSQL ="Delete from Tb_property_model where documentmodel= '"+ entity[0]. Documentmodel +"'"; _dbcontext.database.executesqlcommand (strSQL); }//Convert the collection into a DataTable DataTable dttable=untity.listtodatatable (entity); varSqlconn =(SqlConnection) _dbcontext.database.connection; Sqlconn.open (); using(SqlTransaction transaction =sqlconn.begintransaction ()) { using(SqlBulkCopy bulkcopy =NewSqlBulkCopy (sqlconn, Sqlbulkcopyoptions.default, transaction)) {Bulkcopy.batchsize=10000; Bulkcopy.bulkcopytimeout= -; Bulkcopy.destinationtablename="Tb_property_model"; Try{
This column must be the same as the column of the table in the database. Same as data type. Otherwise, you cannot importforeach(DataColumn Colinchdttable.columns) {bulkCopy.ColumnMappings.Add (col. ColumnName, Col. ColumnName); } bulkcopy.writetoserver (dttable); Transaction.commit (); } Catch(Exception ex) {transaction. Rollback (); Loghelper.error (ex. Message); ThrowEx. innerexception; } finally{bulkcopy.close (); Sqlconn.close (); } } } return true; } Catch(Entityexception ex) {Loghelper.error (ex). Message); ThrowEx. innerexception; } Catch(Dbexception ex) {Loghelper.error (ex). Message); ThrowEx. innerexception; } }
Previously, the data was written by EF. It feels like EF is a bit slow to write, and maybe it's not the right way to find EF.
SQL SERVER Multi-data import