C
C#9mo ago
SWEETPONY

The instance of entity type 'OptimizerInstanceEntity' cannot be tracked

Can someone help me to update model?
public sealed record OptimizerSettingsSetsEntity : OptimizerSettingsSets, IIdentityEntity
{
public new string Identity { get => base.Identity; init => base.Identity = value; }
public List<OptimizerInstanceEntity> OptimizerInstances { get; init; } = [];
public DateTime Created { get; init; }
public Guid Id { get; init; }
public DateTime Timestamp { get; init; }
public DateTime? Updated { get; init; }
}

public sealed record OptimizerInstanceEntity : IdentityEntity
{
public required string Url { get; set; }
}
public sealed record OptimizerSettingsSetsEntity : OptimizerSettingsSets, IIdentityEntity
{
public new string Identity { get => base.Identity; init => base.Identity = value; }
public List<OptimizerInstanceEntity> OptimizerInstances { get; init; } = [];
public DateTime Created { get; init; }
public Guid Id { get; init; }
public DateTime Timestamp { get; init; }
public DateTime? Updated { get; init; }
}

public sealed record OptimizerInstanceEntity : IdentityEntity
{
public required string Url { get; set; }
}
I want to create entity, add it to database and then update only instances My command doesn't work, I got exception after trying to update:
protected override async Task UpdateCoreAsync(OptimizerSettingsSetsModel model, EventHandlingContext context)
{
var exists = await FindByIdentityAsync(model.Identity, context);
if(exists == null)
return;

if(!ChangesComparer.HasChanges(exists.ToModel(), model))
return;

var entity = CreateEntity(model, exists);
var entry = dbContext.OptimizerSettingsSets.Update(entity);
await dbContext.SaveChangesAsync(context.CancellationToken);
entry.State = EntityState.Detached;
await eventDispatcher.OptimizerSettingsSetsUpdatedAsync(entity, context);
}
protected override async Task UpdateCoreAsync(OptimizerSettingsSetsModel model, EventHandlingContext context)
{
var exists = await FindByIdentityAsync(model.Identity, context);
if(exists == null)
return;

if(!ChangesComparer.HasChanges(exists.ToModel(), model))
return;

var entity = CreateEntity(model, exists);
var entry = dbContext.OptimizerSettingsSets.Update(entity);
await dbContext.SaveChangesAsync(context.CancellationToken);
entry.State = EntityState.Detached;
await eventDispatcher.OptimizerSettingsSetsUpdatedAsync(entity, context);
}
2 Replies
SWEETPONY
SWEETPONYOP9mo ago
private OptimizerSettingsSetsEntity CreateEntity(OptimizerSettingsSetsModel model, OptimizerSettingsSetsEntity? entity)
{
return new OptimizerSettingsSetsEntity
{
Id = entity?.Id ?? default,
OptimizerInstances = model.OptimizerInstances?
.Select(m =>
{
var found = dbContext.OptimizerInstances.FirstOrDefault(x => x.Identity == m.Identity);
return new OptimizerInstanceEntity
{
Id = found?.Id ?? default,
Identity = m.Identity,
Url = m.Url
};
}).ToList()!,
OptimizerSettingsParameters = model.OptimizerSettingsParameters?
.Select(op => new OptimizerSettingsParameters
{
Identity = op.Identity,
Value = op.Value,
Description = op.Description
}).ToList()!,
Created = entity?.Created ?? timeProvider.GetUtcNow().DateTime,
Updated = entity == null ? null : timeProvider.GetUtcNow().DateTime,
Identity = model.Identity
};
}
private OptimizerSettingsSetsEntity CreateEntity(OptimizerSettingsSetsModel model, OptimizerSettingsSetsEntity? entity)
{
return new OptimizerSettingsSetsEntity
{
Id = entity?.Id ?? default,
OptimizerInstances = model.OptimizerInstances?
.Select(m =>
{
var found = dbContext.OptimizerInstances.FirstOrDefault(x => x.Identity == m.Identity);
return new OptimizerInstanceEntity
{
Id = found?.Id ?? default,
Identity = m.Identity,
Url = m.Url
};
}).ToList()!,
OptimizerSettingsParameters = model.OptimizerSettingsParameters?
.Select(op => new OptimizerSettingsParameters
{
Identity = op.Identity,
Value = op.Value,
Description = op.Description
}).ToList()!,
Created = entity?.Created ?? timeProvider.GetUtcNow().DateTime,
Updated = entity == null ? null : timeProvider.GetUtcNow().DateTime,
Identity = model.Identity
};
}
entity after this method should be fine.. a new entity contains a list of new instances with new values but existed ids but anyway it doesn't work
Jimmacle
Jimmacle9mo ago
what is the full exception?

Did you find this page helpful?