0

I'm currently working on an ASP .NET 5.0 application - I migrate the app from .NET3.1 to .NET 5.0. I use Entity Framework Core with Devart.Data.Oracle.EFCore to connect to an Oracle Database.

I try to save a simple record with a string value, when I call the Context.SaveChangesAsync() method, I get this error: Sequence contains no elements, oddly the source for the exception is System.Linq => System.Linq.ThrowHelper.ThrowNoElementsException(). There is a StackTrace but no detailed inner exception message unfortunately.

My save method in the repository looks like this:

public async Task<T> Add(T entity)
{
    // ...   
    await Context.AddAsync<T>(entity);
    await Context.SaveChangesAsync();

    return entity;
}

The Tree entity class looks like this:

public class Tree
{
    public int Id { get; set; }
    public string Name { get; set; }
}

The Tree Entity Configuration looks like this (Scaffolded with EF Core):

public void Configure(EntityTypeBuilder<Tree> entity)
{
    entity.ToTable("TREES");

    entity.HasIndex(e => e.Id, "SYS_C001234")
          .IsUnique();

    entity.Property(e => e.Id).HasColumnName("ID");

    entity.Property(e => e.Name).HasColumnName("NAME");
}

On my AppContext, the Trees are registered:

public virtual DbSet<Tree> Trees { get; set; }

In the DB, the Tree table looks like this:

Column_Name | Data_Type
------------------------
ID          | NUMBER(10,0)
NAME        | NCLOB

It is odd, because it worked before I ported my app to .NET 5.0 and updated all NuGet packages...

I tracked down the error to the following NuGet packages updates:

enter image description here

I cannot update Devart.Data.Oracle.EFCore alone, because it is dependent on the Microsoft NuGet packages listed below. I must update all 5 NuGet packages at once.

After this NuGet package update, I cannot save the entity anymore.

Maybe I'm missing something in my configuration?

Do you know how to solve this issue?

azzurro123
  • 521
  • 2
  • 10
  • 19
  • Post the *actual* code that throws, and the *full* exception, not "something like this". It looks like you put a "generic repository" over EF, even though EF already implements not just Repository but Unit-of-Work as well. `SaveChanges` saves *all* pending changes,not just the last one. This makes the `Add` method that tries to save immediately a buggy one - at best, it has very bad performance and breaks UoW. Worst case, it can perform 42 DELETEs and 67 UPDATEs along with that INSERT. – Panagiotis Kanavos Dec 15 '20 at 10:30
  • 1
    To understand why using a "repository" is a bad idea check Gunnar Peipman's [No need for repositories and unit of work with Entity Framework Core](https://gunnarpeipman.com/ef-core-repository-unit-of-work/). As for the exception, `Sequence contains no elements` is typically thrown from `Single()` or other calls that expect to find at least one result. Definitely not `Add` (which doesn't call the database) or `SaveChanges`. The exception's stack trace would show what method was called. – Panagiotis Kanavos Dec 15 '20 at 10:34
  • 1
    This is not a Save issue. You are not connecting to the database. When the application starts it gets latest data from the database. If you do not get any data then you get an error "Sequence contains no elements" when you save. See : https://github.com/oracle/dotnet-db-samples/issues/81 – jdweng Dec 15 '20 at 10:58

1 Answers1

1

The bug with throwing "Sequence contains no elements" on insert or update in EF Core 5 is fixed. We are going to release the new public build of dotConnect for Oracle this week.

Devart
  • 119,203
  • 23
  • 166
  • 186
  • New build of dotConnect for Oracle 9.14.1160 is available for download now: https://forums.devart.com/viewtopic.php?f=1&t=44320. – Devart Dec 17 '20 at 22:11