mtanneryd / ef-bulk-operations

Bulk operations for Entity Framework 6
Apache License 2.0
80 stars 30 forks source link

Bug When Parsing Table Names #18

Closed noopectro closed 4 years ago

noopectro commented 5 years ago

Hello!

I think I've discovered a bug when the extension is parsing out table names.. For my model, when Entity Framework builds the SQL output that the Regex parses in the GetTableName method, for some reason EF is adding an extra space in the FROM clause (between the FROM keyword and the table name), e.g:

FROM  [dbo].[StagingTransactions] AS [Extent1]

When GetTableName parses this, it parses it out as " dbo.StagingTransactions" (with a leading space) and thus returns: Schema = " dbo" TableName = "StagingTransactions"

When this is then returned as a TableName object and parsed back into SQL, it's outputting it as [ dbo].[StagingTransactions] which then causes the Temp Table code to fail.

mtanneryd commented 5 years ago

Hi!

Thanks for the feedback. I have changed the regular expression to be more robust and uploaded 1.2.5-beta1 to nuget. It should be available soon if not already.

mtanneryd commented 5 years ago

Fixed in 1.2.5

vk5880 commented 5 years ago

Hi! Thank you for support. I've found the issue in 1.2.2, I've updated to 1.2.5. But I still have a bug with error:

System.Data.SqlClient.SqlException (0x80131904): Invalid object name 
' Residuals.OfficeResidualItems'.
Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.CreateTempTable(SqlConnection connection, SqlTransaction transaction, TableName tableName, String[] columnNames, IncludeRowNumber includeRowNumber)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkCopy(DbContext ctx, IList entities, Type t, Mappings mappings, SqlTransaction transaction, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkInsertAll(DbContext ctx, IList`1 entities, SqlTransaction sqlTransaction, Boolean recursive, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, Dictionary`2 savedEntities, Dictionary`2 mappingsByType, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkInsertAll(DbContext ctx, IList`1 entities, SqlTransaction sqlTransaction, Boolean recursive, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, Dictionary`2 savedEntities, Dictionary`2 mappingsByType, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.BulkInsertAll[T](DbContext ctx, BulkInsertRequest`1 request)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.BulkInsertAll[T](DbContext ctx, IList`1 entities, SqlTransaction transaction, Boolean recursive)

Could you help with that?

mtanneryd commented 5 years ago

I'll have another look. If you could provide the entity class in question or perhaps the sql code generated by var dbSet = ctx.Set(t); var sql = dbSet.ToString();

vk5880 commented 5 years ago

Sure, Here it is:

SELECT 
    [Extent1].[OfficeResidualItemId] AS [OfficeResidualItemId], 
    [Extent1].[ReportDataId] AS [ReportDataId], 
    [Extent1].[GeneratedReportId] AS [GeneratedReportId], 
    [Extent1].[ReportPeriodId] AS [ReportPeriodId], 
    [Extent1].[OfficeId] AS [OfficeId], 
    [Extent1].[GrossId] AS [GrossId], 
    [Extent1].[ScheduleId] AS [ScheduleId], 
    [Extent1].[AgentPayoutProfileId] AS [AgentPayoutProfileId], 
    [Extent1].[ItemOfficeId] AS [ItemOfficeId], 
    [Extent1].[ItemAgentId] AS [ItemAgentId], 
    [Extent1].[ItemProfileId] AS [ItemProfileId], 
    [Extent1].[ItemId] AS [ItemId], 
    [Extent1].[Transactions] AS [Transactions], 
    [Extent1].[Volume] AS [Volume], 
    [Extent1].[Income] AS [Income], 
    [Extent1].[Total] AS [Total], 
    [Extent1].[Payout] AS [Payout], 
    [Extent1].[ACHCollectedGross] AS [ACHCollectedGross], 
    [Extent1].[ACHCollectedPayout] AS [ACHCollectedPayout], 
    [Extent1].[ACHRejectedGross] AS [ACHRejectedGross], 
    [Extent1].[ACHRejectedPayout] AS [ACHRejectedPayout], 
    [Extent1].[RefundsGross] AS [RefundsGross], 
    [Extent1].[RefundsPayout] AS [RefundsPayout], 
    [Extent1].[NonLiability] AS [NonLiability], 
    [Extent1].[NonLiabilityResiduals] AS [NonLiabilityResiduals], 
    [Extent1].[ReferralResiduals] AS [ReferralResiduals], 
    [Extent2].[ResidualItemId] AS [ResidualItemId]
    FROM  [Residuals].[OfficeResidualItems] AS [Extent1]
    LEFT OUTER JOIN [Residuals].[ResidualItem] AS [Extent2] ON ([Extent2].[OfficeResidualItem_OfficeResidualItemId] IS NOT NULL) AND ([Extent1].[OfficeResidualItemId] = [Extent2].[OfficeResidualItem_OfficeResidualItemId])
vk5880 commented 5 years ago

Before update error was:

System.Data.SqlClient.SqlException (0x80131904): Invalid object name ' Residuals.OfficeResidualItems'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkCopy(DbContext ctx, IList entities, Type t, Mappings mappings, SqlTransaction transaction, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkInsertAll(DbContext ctx, IList`1 entities, SqlTransaction transaction, Boolean recursive, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, Dictionary`2 savedEntities, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkInsertAll(DbContext ctx, IList`1 entities, SqlTransaction transaction, Boolean recursive, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, Dictionary`2 savedEntities, BulkInsertResponse response)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.BulkInsertAll[T](DbContext ctx, BulkInsertRequest`1 request)
   at Tanneryd.BulkOperations.EF6.DbContextExtensions.BulkInsertAll[T](DbContext ctx, IList`1 entities, SqlTransaction transaction, Boolean recursive)
vk5880 commented 5 years ago

something wrong with nuget package? I use <package id="Tanneryd.BulkOperations.EF6" version="1.2.5" targetFramework="net452" /> and dll md5 is 6cbbd4b3a07d1ea641718385c1add3d1 *Tanneryd.BulkOperations.EF6.dll

vk5880 commented 5 years ago

I've built last commit and it worked.

vk5880 commented 5 years ago

Are you going to update Nuget package or I am missing something in my environment?

mtanneryd commented 5 years ago

I’ll look into it tonite and release an update just in case.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd https://www.facebook.com/matkollen

5 sep. 2019 kl. 16:15 skrev vk5880 notifications@github.com:

Are you going to update Nuget package or I am missing something in my invironment

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

vk5880 commented 5 years ago

I've seen some changes, are you going to release an updated package?

mtanneryd commented 5 years ago

Yes, as soon as I can. It has been a busy week.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd https://www.facebook.com/matkollen

6 sep. 2019 kl. 17:36 skrev vk5880 notifications@github.com:

I've seen some changes, are you going to release an updated package?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

mtanneryd commented 5 years ago

There is a 1.2.7-rc1 release uploaded just now. Should be validated and available soon. Let me know if it resolves the issues you've been having.

Thanks,

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den fre 6 sep. 2019 kl 17:36 skrev vk5880 notifications@github.com:

I've seen some changes, are you going to release an updated package?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRMHYKGJQT2EVURSC7LQIJ2HDA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6DG47A#issuecomment-528903804, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2BSRPH24LAG6DQE2UB2VLQIJ2HDANCNFSM4HXKECJQ .

vk5880 commented 5 years ago

Thank you!!!

mtanneryd commented 5 years ago

Posted yet another candidate 1.2.7-rc2, try that one. It contains a more general table name parser.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den tis 10 sep. 2019 kl 20:45 skrev vk5880 notifications@github.com:

Thank you!!!23:30, September 10, 2019, "Måns Tånneryd" < notifications@github.com>:There is a 1.2.7-rc1 release uploaded just now. Should be validated and available soon. Let me know if it resolves the issues you've been having.

Thanks,

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den fre 6 sep. 2019 kl 17:36 skrev vk5880 notifications@github.com:

I've seen some changes, are you going to release an updated package?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub < https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRMHYKGJQT2EVURSC7LQIJ2HDA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6DG47A#issuecomment-528903804 , or mute the thread < https://github.com/notifications/unsubscribe-auth/AA2BSRPH24LAG6DQE2UB2VLQIJ2HDANCNFSM4HXKECJQ

.

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or mute the thread. -- С уважением, Костенко Владислав

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRPJ54QCBZU4FUEHHN3QI7TKTA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6MDOHQ#issuecomment-530069278, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2BSRNYNWNS5TY3XAWZI2LQI7TKTANCNFSM4HXKECJQ .

vk5880 commented 5 years ago

Hi! Now another error: Incorrect syntax near the keyword 'IF'. Database name 'tempdb' ignored, referencing object in tempdb.

mtanneryd commented 5 years ago

Probably IF EXISTS. It might not be supported in all versions of SQL Server. I'll replace it with an alternative.

/Måns

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den mån 23 sep. 2019 kl 17:34 skrev vk5880 notifications@github.com:

Hi! Now another error: Incorrect syntax near the keyword 'IF'. Database name 'tempdb' ignored, referencing object in tempdb.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRNULW3QNZ3U6T2JR3DQLDOZXA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7LI3HQ#issuecomment-534154654, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2BSRPHFOSICMFCMUSVRE3QLDOZXANCNFSM4HXKECJQ .

mtanneryd commented 5 years ago

Published 1.2.7-rc3 where I have rolled back to the old way of detecting if tables exist or not. Sorry about that. Let me know if it resolves your issue.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den mån 23 sep. 2019 kl 17:34 skrev vk5880 notifications@github.com:

Hi! Now another error: Incorrect syntax near the keyword 'IF'. Database name 'tempdb' ignored, referencing object in tempdb.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRNULW3QNZ3U6T2JR3DQLDOZXA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD7LI3HQ#issuecomment-534154654, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2BSRPHFOSICMFCMUSVRE3QLDOZXANCNFSM4HXKECJQ .

vk5880 commented 5 years ago

Looks good now. Works without error. Thank you!

vk5880 commented 5 years ago

Hi, Believe or not it happens again!

[SqlException (0x80131904): Invalid object name 'ResidualsUpload.FDP'.
Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.
Database name 'tempdb' ignored, referencing object in tempdb.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +3302284
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +736
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4061
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +1293
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +421
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +380
   Tanneryd.BulkOperations.EF6.DbContextExtensions.CreateTempTable(SqlConnection connection, SqlTransaction transaction, TableName tableName, String[] columnNames, IncludeRowNumber includeRowNumber) +599
   Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkCopy(DbContext ctx, IList entities, Type t, Mappings mappings, SqlTransaction transaction, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, BulkInsertResponse response) +2050
   Tanneryd.BulkOperations.EF6.DbContextExtensions.DoBulkInsertAll(DbContext ctx, IList`1 entities, SqlTransaction sqlTransaction, Boolean recursive, Boolean allowNotNullSelfReferences, TimeSpan commandTimeout, Dictionary`2 savedEntities, Dictionary`2 mappingsByType, BulkInsertResponse response) +13973
   Tanneryd.BulkOperations.EF6.DbContextExtensions.BulkInsertAll(DbContext ctx, BulkInsertRequest`1 request) +787

table name is FDPASODetailResidualsReports with schema it is ResidualsUpload.FDPASODetailResidualsReports

mtanneryd commented 5 years ago

You’re good at exposing my bugs. :-) I’ll have a look asap.

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd https://www.facebook.com/matkollen

7 okt. 2019 kl. 20:41 skrev vk5880 notifications@github.com:

Hi, Believe or not it happens again!

Invalid object name 'ResidualsUpload.FDP'. Database name 'tempdb' ignored, referencing object in tempdb. Database name 'tempdb' ignored, referencing object in tempdb. Database name 'tempdb' ignored, referencing object in tempdb. table name is FDPASODetailResidualsReports with schema it is ResidualsUpload.FDPASODetailResidualsReports

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

vk5880 commented 5 years ago

Im going to submit a fix with merge request

mtanneryd commented 5 years ago

Great, thanks!

Måns Tånneryd Tånneryd IT AB Barrskogsvägen 19 186 53 Vallentuna +46-705140093 https://se.linkedin.com/in/manstanneryd

https://github.com/mtanneryd/ef6-bulk-operations https://github.com/mtanneryd/ef6-bulk-operations

Den tis 8 okt. 2019 kl 18:17 skrev vk5880 notifications@github.com:

Im going to submit a fix with merge request

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/mtanneryd/ef6-bulk-operations/issues/18?email_source=notifications&email_token=AA2BSRIERRLSEFVP3D27X2TQNSXCZA5CNFSM4HXKECJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAUX6SY#issuecomment-539590475, or mute the thread https://github.com/notifications/unsubscribe-auth/AA2BSROTU4JZAZESIFWZRI3QNSXCZANCNFSM4HXKECJQ .

mtanneryd commented 4 years ago

As far as I can tell this has been resolved. Please reopen if you find this not to be true.