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

Is it possible to configure to increment a property of an entity every time I call to SaveChages()? #32557

Closed
ComptonAlvaro opened this issue Dec 8, 2023 · 1 comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@ComptonAlvaro
Copy link

Hello:

I have an entity in which I want to control concurrency. For that, in the database i have a version field but the entity doesn't need this property for doing its work, so I was thinking if there is some way to configure entity core to control concurrency.

I was thinking to use a shadow property, in this way:

public void Configure(EntityTypeBuilder<MyEntity> paramConfiguration)
{
    paramConfiguration.ToTable("MyTable");


    paramConfiguration.HasKey(o => o.Id);
    paramConfiguration.Property<long>(o => o.Id)
        .HasColumnName("ID")
        .HasColumnType("bigint")
        .IsRequired();

    paramConfiguration.Property<string>(o => o.Name)
        .HasColumnName("Name")
        .HasColumnType("varchar(200)")
        .IsRequired();

    paramConfiguration.Property<long>("Version")
        .HasColumnName("version")
        .HasColumnType("bigint")
        .IsRequired()
        .IsConcurrencyToken();
}

But I would like to know if there is some way to configure Entity Core to update version.

I have seen that HasValueGenerator generates a value when SaveChages() is called, but if I am not wrong, it works only when the entity is added to the context to be added, it doesn't work to update the value of a property of an existing entity.

So perhaps I was thinking something like this:


    public void Configure(EntityTypeBuilder<MyEntity> paramConfiguration)
    {
        paramConfiguration.ToTable("MyTable");


        paramConfiguration.HasKey(o => o.Id);
        paramConfiguration.Property<long>(o => o.Id)
            .HasColumnName("ID")
            .HasColumnType("bigint")
            .IsRequired();

        paramConfiguration.Property<string>(o => o.Name)
            .HasColumnName("Name")
            .HasColumnType("varchar(200)")
            .IsRequired();

        paramConfiguration.Property<long>("Version")
            .HasColumnName("version")
            .HasColumnType("bigint")
            .IsRequired()
            .IsConcurrencyToken(() => UpdateConcurrencyProperty());
    }


    private long UndateConcurrencyProperty()
    {
        return ((long)context.Entry(std).Property("Version").CurrentValue) + 1;
    }

In this way, I could handle concurrency in only once place, in the repository. Because if not, I would need to add a property to my domain entity and increase the value in the domain each time I modify the root entity or I handle some of its children. But I also I have to configure Entity Core to handle concurrency setting the version property. So I have this in two places.

If Entity Core would allow to increase the version property, I could avoid to have a version property in the domain, that it is not really its responsability, and I avoid too to have to configure the concurrency in two places, the domain and the repository.

It is true that in this case the version will be increased each time i call the SaveChages(), but in my case it is not a problem, because I have a repository only for commands, and I istantiate a repository each time I perform an action, so the entities that there is in the repository I will be needed to increase the version.

So in sumary, is it possible to configure the shadow property to be a concurrency token and increase its value each time I call the SaveChanges()?

Thanks.

@ajcvickers
Copy link
Contributor

Covered in spirit by #2195

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jan 3, 2024
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants