'Azure Cosmos DB - StartIndex cannot be less than zero. (Parameter 'startIndex')

I'm trying to insert a clan entity to Clan container. My partition key is id and the model is like this:

public class Clan
{
    public string Id { get; set; }
    public string ClanName { get; set; }
    public int MembersCount { get; set; }
    public int Capacity { get; set; }
    public int WHP { get; set; }
    public int LogoId { get; set; }
    public AppMarketType AppMarket { get; set; }
}

ClanDbContext:

public class ClanContext : DbContext
{
    public ClanContext(DbContextOptions<ClanContext> options)
        : base(options) { }

    public DbSet<Clan> Clans { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Clan>()
            .HasNoDiscriminator()
            .ToContainer("Clans")
            .HasPartitionKey(p => p.Id)
            .Property(x => x.Id).ToJsonProperty("id");
    }
}

All I want to do is:

try
{
    var clan = new Clan
    {
        Id = Guid.NewGuid().ToString(),
        AppMarket = Core.AppMarketType.GooglePlay,
        ClanName = "Blazers",
        Capacity = 110,
        LogoId = 342,
        MembersCount = 34,
        WHP = 1280
    };
    _clanContext.Add(clan);
    _clanContext.SaveChanges();
 }
 catch (Exception)
 {
     throw;
 }

It throws an exception:

StartIndex cannot be less than zero. (Parameter 'startIndex')

Callstack:

at System.Text.StringBuilder.Remove(Int32 startIndex, Int32 length)
at Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration.Internal.IdValueGenerator.NextValue(EntityEntry entry)
at Microsoft.EntityFrameworkCore.ValueGeneration.ValueGenerator.Next(EntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ValueGenerationManager.Generate(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
at Microsoft.EntityFrameworkCore.DbContext.Add[TEntity](TEntity entity)
at Dummy.Web.Services.ClanService.<IncreaseWHP>d__6.MoveNext() in C:\Repositories\dummysolution\Domain\dummydomain\Services\ClanService.cs:line 50
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Clans.JoinGroup.<Run>d__2.MoveNext() in C:\Repositories\dummysolution\API\dummyapi\Clans\JoinGroup.cs:line 55


Solution 1:[1]

After confirm with contributor, this issue was fixed in Microsoft.EntityFrameworkCore.Cosmos 5.0.0.

If you are using a version before 5.0.0, please try after upgrading.

If you are getting a similar exception in 5.0.0 or later please file a new issue and include a small project that demonstrates it.


Related Issue:

Cannot create new item in collection

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason