dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

Unit Tests Failed To Seed Initial Data #13405

Closed e88z4 closed 2 years ago

e88z4 commented 6 years ago

Describe what is not working as expected.

I have a unit test project. The unit test workflow works as below:

  1. Ensure database is created.
  2. Data seed
  3. Run test case
  4. Delete database

My target database is my own localdb. The database is drop and recrecate for each unit test.

When I ran the test case individually, it works as expected. When I ran them all at once, the unit test failed to seed the data on #2 with the exception below.

Running all unit tests at once

Error message: Message: Test method MergeDbEtlUnitTest.CreateDiffProcessorTest.InsertDropOffPolicies_PolicyExists_NotToBeAddedToDropOffPolicy threw exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'ProcessHistory_t' when IDENTITY_INSERT is set to OFF.

Result StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 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.SqlDataReader.TryHasMoreRows(Boolean& moreRows) at System.Data.SqlClient.SqlDataReader.TryHasMoreResults(Boolean& moreResults) at System.Data.SqlClient.SqlDataReader.TryNextResult(Boolean& more) at System.Data.SqlClient.SqlDataReader.NextResult() at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader) --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(DbContext _, ValueTuple2 parameters) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable1 commandBatches, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList1 entries) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList1 entriesToSave) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) at Microsoft.EntityFrameworkCore.DbContext.SaveChanges() at MergeDbEtlUnitTest.CreateDiffProcessorTest.InsertDropOffPolicies_DataSeed() in C:\tfs\UK_SOLV_Applications\MergeDb\Solutions\MergeDbEtlUnitTest\CreateDiffProcessorTest.cs:line 123 at MergeDbEtlUnitTest.CreateDiffProcessorTest.InsertDropOffPolicies_PolicyExists_NotToBeAddedToDropOffPolicy() in C:\tfs\UK_SOLV_Applications\MergeDb\Solutions\MergeDbEtlUnitTest\CreateDiffProcessorTest.cs:line 173 Result Message: Test method MergeDbEtlUnitTest.CreateDiffProcessorTest.InsertDropOffPolicies_PolicyExists_NotToBeAddedToDropOffPolicy threw exception: Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert explicit value for identity column in table 'ProcessHistory_t' when IDENTITY_INSERT is set to OFF.

Source Code

public void InsertDropOffPolicies_DataSeed()
        {            
            using (var context = new MergeDbDatabaseContext())
            {
                List<EtlSourceFile> etlHistoryFile1 = new List<EtlSourceFile>
                {
                    new PayMasterFile() { FileName = $"Test1PayMasterFile.txt", FileSHA256 = "abc", InternalFileDate = DateTime.UtcNow },
                    new PaymentFile() { FileName = "Test1PaymentFile.txt", FileSHA256 = "zzz" }
                };

                List<EtlSourceFile> etlHistoryFile2 = new List<EtlSourceFile>
                {
                    new PayMasterFile() { FileName = $"Test2PayMasterFile.txt", FileSHA256 = "def", InternalFileDate = DateTime.UtcNow },
                    new PaymentFile() { FileName = "Test2PaymentFile.txt", FileSHA256 = "yyy" }
                };

                _mergeDbEtlProcessHistories.Add(new MergeDbEtlProcessHistory()
                {                    
                    ProcessName = "UnitTest_InsertDropOffPOlicy",
                    SourceFiles = etlHistoryFile1,
                    Status = Status.COMPLETED,
                    ZipFileName = "Input1.txt",
                    ZipFileSha256Hash = "zzz"
                });

                _mergeDbEtlProcessHistories.Add(new MergeDbEtlProcessHistory()
                {                 
                    ProcessName = "UnitTest_InsertDropOffPOlicy",
                    SourceFiles = etlHistoryFile2,
                    Status = Status.RUNNING,
                    ZipFileName = "Input2.txt",
                    ZipFileSha256Hash = "zzz"
                });

                context.MergeDbEtlProcessHistories.AddRange(_mergeDbEtlProcessHistories);

                context.Policies.Add(new Policy() { ElementNo = $"001", BenStatus = "C" });
                context.Policies.Add(new Policy() { ElementNo = $"002", BenStatus = "C" });
                context.Policies.Add(new Policy() { ElementNo = $"003", BenStatus = "c" });
                context.Policies.Add(new Policy() { ElementNo = $"004", BenStatus = "D" });
                context.Policies.Add(new Policy() { ElementNo = $"002", BenStatus = "C" });
                context.Policies.Add(new Policy() { ElementNo = $"004", BenStatus = "D" });
                context.Policies.Add(new Policy() { ElementNo = $"005", BenStatus = "D" });

                context.SaveChanges(); // Exception occurs on this line. 

                context.PayMasterRecords.Add(new PayMasterRecord()
                {
                    ElementNo = $"005",
                    SourceFileId = _mergeDbEtlProcessHistories.First(x => x.ZipFileName == "Input1.txt").SourceFiles
                        .First(x => x.GetType() == typeof(PayMasterFile)).Id
                });

                context.PayMasterRecords.Add(new PayMasterRecord()
                {
                    ElementNo = $"005",
                    SourceFileId = _mergeDbEtlProcessHistories.First(x => x.ZipFileName == "Input2.txt").SourceFiles
                        .First(x => x.GetType() == typeof(PayMasterFile)).Id
                });

                context.SaveChanges();
            }
        }

Further technical details

EF Core version: EF core 2.1.3 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 7 IDE: (e.g. Visual Studio 2017 15.4)

SQL Trace:

exec sp_executesql N'SET NOCOUNT ON; DECLARE @inserted0 TABLE ([Id] bigint, [_Position] [int]);

DECLARE @inserted7 TABLE ([Id] int, [_Position] [int]); MERGE [ProcessHistory_t] USING ( VALUES (@p210, @p211, @p212, @p213, @p214, @p215, @p216, 0), (@p217, @p218, @p219, @p220, @p221, @p222, @p223, 1)) AS i ([Id], [Discriminator], [ProcessName], [StartDateTime], [Status], [ZipFileName], [ZipFileSha256Hash], _Position) ON 1=0 WHEN NOT MATCHED THEN INSERT ([Id], [Discriminator], [ProcessName], [StartDateTime], [Status], [ZipFileName], [ZipFileSha256Hash]) VALUES (i.[Id], i.[Discriminator], i.[ProcessName], i.[StartDateTime], i.[Status], i.[ZipFileName], i.[ZipFileSha256Hash]) OUTPUT INSERTED.[Id], i._Position INTO @inserted7;

',N'@p0 datetime2(7),@p1 nvarchar(4000),@p2 nvarchar(4000),@p3 int,@p4 int,@p5 date,@p6 nvarchar(4000),@p7 date,@p8 date,@p9 date,@p10 date,@p11 nvarchar(450),@p12 nvarchar(4000),@p13 nvarchar(4000),@p14 nvarchar(4000),@p15 nvarchar(4000),@p16 int,@p17 nvarchar(4000),@p18 nvarchar(4000),@p19 int,@p20 nvarchar(4000),@p21 nvarchar(4000),@p22 nvarchar(4000),@p23 nvarchar(4000),@p24 int,@p25 int,@p26 nvarchar(4000),@p27 nvarchar(4000),@p28 nvarchar(4000),@p29 nvarchar(4000),@p30 datetime2(7),@p31 nvarchar(4000),@p32 nvarchar(4000),@p33 int,@p34 int,@p35 date,@p36 nvarchar(4000),@p37 date,@p38 date,@p39 date,@p40 date,@p41 nvarchar(450),@p42 nvarchar(4000),@p43 nvarchar(4000),@p44 nvarchar(4000),@p45 nvarchar(4000),@p46 int,@p47 nvarchar(4000),@p48 nvarchar(4000),@p49 int,@p50 nvarchar(4000),@p51 nvarchar(4000),@p52 nvarchar(4000),@p53 nvarchar(4000),@p54 int,@p55 int,@p56 nvarchar(4000),@p57 nvarchar(4000),@p58 nvarchar(4000),@p59 nvarchar(4000),@p60 datetime2(7),@p61 nvarchar(4000),@p62 nvarchar(4000),@p63 int,@p64 int,@p65 date,@p66 nvarchar(4000),@p67 date,@p68 date,@p69 date,@p70 date,@p71 nvarchar(450),@p72 nvarchar(4000),@p73 nvarchar(4000),@p74 nvarchar(4000),@p75 nvarchar(4000),@p76 int,@p77 nvarchar(4000),@p78 nvarchar(4000),@p79 int,@p80 nvarchar(4000),@p81 nvarchar(4000),@p82 nvarchar(4000),@p83 nvarchar(4000),@p84 int,@p85 int,@p86 nvarchar(4000),@p87 nvarchar(4000),@p88 nvarchar(4000),@p89 nvarchar(4000),@p90 datetime2(7),@p91 nvarchar(4000),@p92 nvarchar(4000),@p93 int,@p94 int,@p95 date,@p96 nvarchar(4000),@p97 date,@p98 date,@p99 date,@p100 date,@p101 nvarchar(450),@p102 nvarchar(4000),@p103 nvarchar(4000),@p104 nvarchar(4000),@p105 nvarchar(4000),@p106 int,@p107 nvarchar(4000),@p108 nvarchar(4000),@p109 int,@p110 nvarchar(4000),@p111 nvarchar(4000),@p112 nvarchar(4000),@p113 nvarchar(4000),@p114 int,@p115 int,@p116 nvarchar(4000),@p117 nvarchar(4000),@p118 nvarchar(4000),@p119 nvarchar(4000),@p120 datetime2(7),@p121 nvarchar(4000),@p122 nvarchar(4000),@p123 int,@p124 int,@p125 date,@p126 nvarchar(4000),@p127 date,@p128 date,@p129 date,@p130 date,@p131 nvarchar(450),@p132 nvarchar(4000),@p133 nvarchar(4000),@p134 nvarchar(4000),@p135 nvarchar(4000),@p136 int,@p137 nvarchar(4000),@p138 nvarchar(4000),@p139 int,@p140 nvarchar(4000),@p141 nvarchar(4000),@p142 nvarchar(4000),@p143 nvarchar(4000),@p144 int,@p145 int,@p146 nvarchar(4000),@p147 nvarchar(4000),@p148 nvarchar(4000),@p149 nvarchar(4000),@p150 datetime2(7),@p151 nvarchar(4000),@p152 nvarchar(4000),@p153 int,@p154 int,@p155 date,@p156 nvarchar(4000),@p157 date,@p158 date,@p159 date,@p160 date,@p161 nvarchar(450),@p162 nvarchar(4000),@p163 nvarchar(4000),@p164 nvarchar(4000),@p165 nvarchar(4000),@p166 int,@p167 nvarchar(4000),@p168 nvarchar(4000),@p169 int,@p170 nvarchar(4000),@p171 nvarchar(4000),@p172 nvarchar(4000),@p173 nvarchar(4000),@p174 int,@p175 int,@p176 nvarchar(4000),@p177 nvarchar(4000),@p178 nvarchar(4000),@p179 nvarchar(4000),@p180 datetime2(7),@p181 nvarchar(4000),@p182 nvarchar(4000),@p183 int,@p184 int,@p185 date,@p186 nvarchar(4000),@p187 date,@p188 date,@p189 date,@p190 date,@p191 nvarchar(450),@p192 nvarchar(4000),@p193 nvarchar(4000),@p194 nvarchar(4000),@p195 nvarchar(4000),@p196 int,@p197 nvarchar(4000),@p198 nvarchar(4000),@p199 int,@p200 nvarchar(4000),@p201 nvarchar(4000),@p202 nvarchar(4000),@p203 nvarchar(4000),@p204 int,@p205 int,@p206 nvarchar(4000),@p207 nvarchar(4000),@p208 nvarchar(4000),@p209 nvarchar(4000),@p210 int,@p211 nvarchar(4000),@p212 nvarchar(50),@p213 datetime2(7),@p214 int,@p215 nvarchar(255),@p216 nvarchar(64),@p217 int,@p218 nvarchar(4000),@p219 nvarchar(50),@p220 datetime2(7),@p221 int,@p222 nvarchar(255),@p223 nvarchar(64),@p224 nvarchar(4000),@p225 nvarchar(50),@p226 int,@p227 nvarchar(255),@p228 nvarchar(64),@p229 nvarchar(4000),@p230 nvarchar(50),@p231 int,@p232 nvarchar(255),@p233 nvarchar(64)',@p0=NULL,@p1=NULL,@p2=N'C',@p3=0,@p4=0,@p5=NULL,@p6=NULL,@p7=NULL,@p8=NULL,@p9=NULL,@p10=NULL,@p11=N'001',@p12=NULL,@p13=NULL,@p14=NULL,@p15=NULL,@p16=NULL,@p17=NULL,@p18=NULL,@p19=0,@p20=NULL,@p21=NULL,@p22=NULL,@p23=NULL,@p24=0,@p25=0,@p26=NULL,@p27=NULL,@p28=NULL,@p29=NULL,@p30=NULL,@p31=NULL,@p32=N'C',@p33=0,@p34=0,@p35=NULL,@p36=NULL,@p37=NULL,@p38=NULL,@p39=NULL,@p40=NULL,@p41=N'002',@p42=NULL,@p43=NULL,@p44=NULL,@p45=NULL,@p46=NULL,@p47=NULL,@p48=NULL,@p49=0,@p50=NULL,@p51=NULL,@p52=NULL,@p53=NULL,@p54=0,@p55=0,@p56=NULL,@p57=NULL,@p58=NULL,@p59=NULL,@p60=NULL,@p61=NULL,@p62=N'c',@p63=0,@p64=0,@p65=NULL,@p66=NULL,@p67=NULL,@p68=NULL,@p69=NULL,@p70=NULL,@p71=N'003',@p72=NULL,@p73=NULL,@p74=NULL,@p75=NULL,@p76=NULL,@p77=NULL,@p78=NULL,@p79=0,@p80=NULL,@p81=NULL,@p82=NULL,@p83=NULL,@p84=0,@p85=0,@p86=NULL,@p87=NULL,@p88=NULL,@p89=NULL,@p90=NULL,@p91=NULL,@p92=N'D',@p93=0,@p94=0,@p95=NULL,@p96=NULL,@p97=NULL,@p98=NULL,@p99=NULL,@p100=NULL,@p101=N'004',@p102=NULL,@p103=NULL,@p104=NULL,@p105=NULL,@p106=NULL,@p107=NULL,@p108=NULL,@p109=0,@p110=NULL,@p111=NULL,@p112=NULL,@p113=NULL,@p114=0,@p115=0,@p116=NULL,@p117=NULL,@p118=NULL,@p119=NULL,@p120=NULL,@p121=NULL,@p122=N'C',@p123=0,@p124=0,@p125=NULL,@p126=NULL,@p127=NULL,@p128=NULL,@p129=NULL,@p130=NULL,@p131=N'002',@p132=NULL,@p133=NULL,@p134=NULL,@p135=NULL,@p136=NULL,@p137=NULL,@p138=NULL,@p139=0,@p140=NULL,@p141=NULL,@p142=NULL,@p143=NULL,@p144=0,@p145=0,@p146=NULL,@p147=NULL,@p148=NULL,@p149=NULL,@p150=NULL,@p151=NULL,@p152=N'D',@p153=0,@p154=0,@p155=NULL,@p156=NULL,@p157=NULL,@p158=NULL,@p159=NULL,@p160=NULL,@p161=N'004',@p162=NULL,@p163=NULL,@p164=NULL,@p165=NULL,@p166=NULL,@p167=NULL,@p168=NULL,@p169=0,@p170=NULL,@p171=NULL,@p172=NULL,@p173=NULL,@p174=0,@p175=0,@p176=NULL,@p177=NULL,@p178=NULL,@p179=NULL,@p180=NULL,@p181=NULL,@p182=N'D',@p183=0,@p184=0,@p185=NULL,@p186=NULL,@p187=NULL,@p188=NULL,@p189=NULL,@p190=NULL,@p191=N'005',@p192=NULL,@p193=NULL,@p194=NULL,@p195=NULL,@p196=NULL,@p197=NULL,@p198=NULL,@p199=0,@p200=NULL,@p201=NULL,@p202=NULL,@p203=NULL,@p204=0,@p205=0,@p206=NULL,@p207=NULL,@p208=NULL,@p209=NULL,@p210=1,@p211=N'MergeDbEtlProcessHistory',@p212=N'UnitTest_InsertDropOffPOlicy',@p213='2018-09-24 18:23:01.2466667',@p214=2,@p215=N'Input1.txt',@p216=N'zzz',@p217=2,@p218=N'MergeDbEtlProcessHistory',@p219=N'UnitTest_InsertDropOffPOlicy',@p220='2018-09-24 18:23:01.2466667',@p221=0,@p222=N'Input2.txt',@p223=N'zzz',@p224=N'MergeDbEtlProcessHistory',@p225=N'UnitTest_InsertDropOffPOlicy',@p226=2,@p227=N'Input1.txt',@p228=N'zzz',@p229=N'MergeDbEtlProcessHistory',@p230=N'UnitTest_InsertDropOffPOlicy',@p231=0,@p232=N'Input2.txt',@p233=N'zzz'

ajcvickers commented 6 years ago

Duplicate of #703