Base solution for your next web application
Open Closed

Setting navigation properties when using IRepository in tests results in attempted duplications #9912


User avatar
0
aescg1220 created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version? 10.0.0
  • What is your product type (Angular or MVC)? Angular
  • What is product framework type (.net framework or .net core)? .net core

If issue related with ABP Framework

  • What is ABP Framework version? 6.0.0

Issue

I have 2 entities in my model

Now i've added a test using repositories:

Unfortunately the test fails on line 27, due to the fact that it tries to recreate the SimpleEntity (with the same Id). var barId = await bars.InsertAndGetIdAsync(new Bar() { Name = "Bar1", SimpleEntity = simpleEntity });

Should we be able to set the navigation property when using repositories? Is this a limitation of IRepository usage in Tests?

Notes

Setting the navigation property works when using the UsingDbContext method Setting the SimpleEntityId property works when using repositories Creating the simpleentity inline works when using repositories (e.g. var barId = await bars.InsertAndGetIdAsync(new Bar() { Name = "Bar1", SimpleEntity = new SimpleEntity() { Name = "Simple1" } });)

Exception

Source: BarTests.cs line 19 Duration: 5.6 sec

Message: Microsoft.EntityFrameworkCore.DbUpdateException : An error occurred while updating the entries. See the inner exception for details. ---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 19: 'UNIQUE constraint failed: SimpleEntities.Id'. Stack Trace: ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) BatchExecutor.ExecuteAsync(IEnumerable1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken) StateManager.SaveChangesAsync(IList1 entriesToSave, CancellationToken cancellationToken) StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) AbpDbContext.SaveChangesAsync(CancellationToken cancellationToken) AbpZeroCommonDbContext3.SaveChangesAsync(CancellationToken cancellationToken) EfCoreRepositoryBase3.InsertAndGetIdAsync(TEntity entity) UnitOfWorkInterceptor.InternalInterceptAsynchronous[TResult](IInvocation invocation) BarTests.CanSetDependenciesSeparately() line 27 --- End of stack trace from previous location --- ----- Inner Stack Trace ----- SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) SqliteDataReader.NextResult() SqliteCommand.ExecuteReader(CommandBehavior behavior) SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) SqliteCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken) RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)


2 Answer(s)
  • User Avatar
    1
    aescg1220 created

    Got it figured out... Simply wrap the code in WithUnitOfWork/WithUnitOfWorkAsync

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @aescg1220 :)