Entity Framework:entityTypeConfiguration仅在更新时设置属性值。

huangapple go评论63阅读模式
英文:

Entity Framework : entityTypeConfiguration set prop value only on update

问题

I would expect that when using ValueGeneratedOnUpdate, the specific value generator would only be called if the entity has been updated (so the entity already exists).

Instead it seems that the value is generated on add as well.

This is how I'm configuring the create and update props:

builder.Property(self => self.CreatedAt)
    .ValueGeneratedOnAdd()
    .HasValueGenerator<UtcTimeValueGenerator>();

builder.Property(self => self.UpdatedAt)
    .ValueGeneratedOnUpdate()
    .HasDefaultValue(null)
    .HasValueGenerator<UtcTimeValueGenerator>();

Seed data:

builder.HasData(new List<Device>
{
    new Device
    {
        Id = 1,
        ParkingLotId = 1,
        Serial = "test",
        SigfoxSerial = "test"
    }
});

Generator:

public class UtcTimeValueGenerator : ValueGenerator<DateTime>
{
    public override bool GeneratesTemporaryValues => false;

    public override DateTime Next(EntityEntry entry)
        => DateTime.UtcNow;
}

Using IEntityTypeConfiguration<T> only, can a datetime property be set for a specific column only if the entry already exists.

英文:

I would expect that when using ValueGeneratedOnUpdate the specific value generator would only be called if the entity has been updated (so the entity already exists).

Instead it seems that the value is generated on add as well.

This is how I'm configuring the create and update props

builder.Property(self =&gt; self.CreatedAt)
    .ValueGeneratedOnAdd()
    .HasValueGenerator&lt;UtcTimeValueGenerator&gt;();

builder.Property(self =&gt; self.UpdatedAt)
    .ValueGeneratedOnUpdate()
    .HasDefaultValue(null)
    .HasValueGenerator&lt;UtcTimeValueGenerator&gt;();

Seed data.

builder.HasData(new List&lt;Device&gt;
{
    new Device
    {
        Id = 1,
        ParkingLotId = 1,
        Serial = &quot;test&quot;,
        SigfoxSerial = &quot;test&quot;
    }
});

Generator

public class UtcTimeValueGenerator : ValueGenerator&lt;DateTime&gt;
{
    public override bool GeneratesTemporaryValues =&gt; false;

    public override DateTime Next(EntityEntry entry)
        =&gt; DateTime.UtcNow;
}

Using IEntityTypeConfiguration<T> only, can a datetime propery be set for a specific column only if the entry already exist.

答案1

得分: 0

相反,您可以使用 Nullable<DateTime> 生成器。在 Next 函数中,您可以检查状态并决定是否生成新值。

public class UtcTimeValueGenerator : ValueGenerator<DateTime?>
{
    public override bool GeneratesTemporaryValues => false;

    public override DateTime? Next(EntityEntry entry)
        => entry.State == EntityState.Modified ? DateTime.UtcNow : null;
}
英文:

Instead, you can use a Nullable&lt;DateTime&gt; generator. And in the Next function you can check the state and decide whether to generate a new value or not.

public class UtcTimeValueGenerator : ValueGenerator&lt;DateTime?&gt;
{
    public override bool GeneratesTemporaryValues =&gt; false;

    public override DateTime Next(EntityEntry entry)
        =&gt; entry.State == EntityState.Modified ? DateTime.UtcNow : null;
}

</details>



huangapple
  • 本文由 发表于 2023年2月6日 20:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定