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

Property defining expressions for update #10862

Open
Tracked by #22953
ajcvickers opened this issue Feb 2, 2018 · 4 comments
Open
Tracked by #22953

Property defining expressions for update #10862

ajcvickers opened this issue Feb 2, 2018 · 4 comments

Comments

@ajcvickers
Copy link
Member

A common request is to have a server-side auto-generated timestamp for any update. One way this could be implemented is through an expression defined on the property to run when that property is saved to the database. Something like:

.Property(e => e.UpdatedOn).HasUpdateSql("GetDate()");

or even

.Property(e => e.UpdatedOn).HasUpdateExpression(e => DateTime.Now);

which will then be translated.

Note that this is very similar to server-side value conversions--see #10861--except that it may not be a "conversion".

(Doors to manual and cross-check with #10768)

@CoskunSunali
Copy link

This would be super useful and not only for update dates but for a million things. E.g. LastUpdatedBy.

@TanvirArjel
Copy link

@ajcvickers Believe me! This is one of the most wanted feature for me in EF Core 5.0. I think this should not take so much time to implement but this would be a very very useful feature and frequently used feature in EF Core.

Please implement this is in EF Core 5.0.

@Seabizkit
Copy link

but you can basically implement this by overriding saveChanges and casting your entities to a share base or asking if it has the property and then setting it before change,

for example

internal static void AddTimestamps(BaseEntity entity, CurrentUser currentUser)
        {
            if (entity == null)
                return;

            if (entity.Id == 0)
            {
                entity.UserCreatedId = currentUser.UserId;
                entity.UserCreated = currentUser.UserName;
                entity.DateCreated = DateTime.UtcNow;
            }
            else
            {
                entity.UserModifiedId = currentUser.UserId;
                entity.UserModified = currentUser.UserName;
                entity.DateModified = DateTime.UtcNow;
            }
        }

just saying there are better things to focus energy on in my opinion

@TanvirArjel
Copy link

@ajcvickers This can be a very good candidate for EF Core 6.0.

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

No branches or pull requests

5 participants