Skip to content

Migration seed data does not respect "ValueGeneratedOnAddOrUpdate" #21904

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
thehe opened this issue Aug 3, 2020 · 3 comments
Closed

Migration seed data does not respect "ValueGeneratedOnAddOrUpdate" #21904

thehe opened this issue Aug 3, 2020 · 3 comments

Comments

@thehe
Copy link

thehe commented Aug 3, 2020

If you're using Seed-Data with EF.Migrations with Value-Generators, initial values are not created with "ValueGeneratedOnAddOrUpdate()" but only with "ValueGeneratedOnAdd()".

see my PR: #21865
Problem seems to be the equality-comparison of a flag enum!

Steps to reproduce

TimedEntity.cs (abstract)

    public abstract class TimedEntity
    {
        [Column("created_at")]
        public DateTimeOffset CreatedAt { get; }

        [Column("updated_at")]
        public DateTimeOffset UpdatedAt { get; } // notice: not nullable here!
    }

Voucher.cs

    [Table("vouchers")]
    public class Voucher : TimedEntity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Column("id")]
        [Key]
        public long Id { get; set; }

        [Column("code"), StringLength(100)]
        public string Code { get; set; }
   }

Migrations seed data - OnConfiguring:

            var voucher = modelBuilder.Entity<Voucher>();
            voucher.Property(x => x.CreatedAt).ValueGeneratedOnAdd().HasValueGenerator<NowValueGenerator>();
            voucher.Property(x => x.UpdatedAt).ValueGeneratedOnAddOrUpdate().HasValueGenerator<NowValueGenerator>(); // notice: generate value on ADD *OR* UPDATE!

            voucher.HasData(new Voucher()
            {
                Code = "ABC1234",
                Id = 1
            });
           // no definition of createdAt or updatedAt here - should be filled out automatically

Results in:

            migrationBuilder.InsertData(
                table: "vouchers",
                columns: new[] { "id", "created_at", "code" },  // notice: "updated_at" missing here!
                values: new object[,]
                {
                    { 1L, new DateTimeOffset(new DateTime(2020, 8, 2, 22, 42, 6, 105, DateTimeKind.Unspecified).AddTicks(4450), "ABC1234" },
                });

Further technical details

EF Core version: 3.1.6
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL (but irrelevant)
Target framework: .NET Core 3.1
Operating system: RHEL7, RHEL8, Win10 Enterprise 20H2
IDE: VSCode, Visual Studio 2019 pro

@AndriySvyryd
Copy link
Member

@thehe ValueGenerated configuration only affects values generated by the database, you don't need to specify them when using a client value generator. Also a value generator isn't currently used when updating, see #6999

@thehe
Copy link
Author

thehe commented Aug 3, 2020

@AndriySvyryd I'm quite confused now:

Why there is "created_at" then in the seed data and not "updated_at"?
When it shouldn't do anything regarding the client-side ValueGenerator, then there shouldn't be any difference between resulting "createdAt" and "updatedAt"?
But "updatedAt" is just missing in seed data.

Can you please describe what's preventing "updated_at" to get popoluted by the same exact value-generator (which just pushes out "DateTimeOffset.UtcNow" as value)

@AndriySvyryd
Copy link
Member

When you configure a property as ValueGeneratedOnAddOrUpdate EF assumes that the value will always be provided by the database, therefore one supplied in the insert operation would either be ignored or cause an error. However for ValueGeneratedOnAdd many databases do provide a way of provided a value that would override what would be generated by the database.

Usually the table auditing pattern is implemented purely on the database side, as that provides the most consistent values.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants