Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using EF Core 8 Complex Property results in error "Sequence contains no matching element" #1489

Open
Rookian opened this issue May 15, 2024 · 0 comments

Comments

@Rookian
Copy link

Rookian commented May 15, 2024

 var dbContext = GetDbContext();
 await dbContext.Database.EnsureDeletedAsync();
 await dbContext.Database.EnsureCreatedAsync();
 
 var tagsToSave = new List<Tag>()
 {
     new() { Name = "Tag1", AuditingInfo = new AuditingInfo("A", DateTimeOffset.Now, "B", DateTimeOffset.Now) }
 };
 var bulkConfig = new BulkConfig
 {
     UseTempDB = true,
     BulkCopyTimeout = 0,
     UpdateByProperties = new List<string>
     {
         // When we used owned properties, we left out nameof(Tag.AuditingInfo)
         // When I now leave out nameof(Tag.AuditingInfo), we get the error "Cannot insert the value NULL into column 'AuditingInfo_CreatedAt'"
         // When we specify nameof(Tag.AuditingInfo) we get the error "Sequence contains no matching element"
         nameof(Tag.Name), nameof(Tag.AuditingInfo)
     }
 };

 await dbContext.Database.CreateExecutionStrategy()
     .ExecuteInTransactionAsync(
         () => dbContext.BulkInsertOrUpdateAsync(tagsToSave, bulkConfig),
         () => Task.FromResult(true));
[ComplexType]
public class AuditingInfo
{
    public AuditingInfo(string createdBy, DateTimeOffset createdAt, string updatedBy, DateTimeOffset updatedAt)
    {
        CreatedBy = createdBy;
        CreatedAt = createdAt;
        UpdatedBy = updatedBy;
        UpdatedAt = updatedAt;
    }

    [MaxLength(100)]
    public string CreatedBy { get; private set; }

    public DateTimeOffset CreatedAt { get; private set; }

    [MaxLength(100)]
    public string UpdatedBy { get; private set; }

    public DateTimeOffset UpdatedAt { get; private set; }
}

public class Tag : Entity
{

    public ulong RowVersion { get; set; }

    [Column(TypeName = "nvarchar(100)")]
    public string Name { get; set; } = null!;
    public AuditingInfo AuditingInfo { get; set; } = null!;
}

Stack trace:

System.InvalidOperationException
Sequence contains no matching element
at System.Linq.ThrowHelper.ThrowNoMatchException()
at System.Linq.Enumerable.First[TSource](IEnumerable1 source, Func2 predicate)
at EFCore.BulkExtensions.TableInfo.<>c__DisplayClass189_01.<LoadData>b__3(String b) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](List1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector)
at EFCore.BulkExtensions.TableInfo.LoadData[T](DbContext context, Type type, IEnumerable1 entities, Boolean loadOnlyPKColumn) at EFCore.BulkExtensions.TableInfo.CreateInstance[T](DbContext context, Type type, IEnumerable1 entities, OperationType operationType, BulkConfig bulkConfig)
at EFCore.BulkExtensions.DbContextBulkTransaction.ExecuteAsync[T](DbContext context, Type type, IEnumerable1 entities, OperationType operationType, BulkConfig bulkConfig, Action1 progress, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass20_01.<<ExecuteInTransactionAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass24_02.<b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass24_02.<<ExecuteInTransactionAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at SampleTest.Should() in ***\SampleTest.cs:line 57

Used version: 8.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant