英文:
How to automatically generate fields using Entity Framework when adding and updating records?
问题
I'm having trouble making the "CreatedAt" and "ModifiedAt" fields automatically generated by Entity Framework when adding and updating records in a table. I've tried using the "ValueGeneratedOnAddOrUpdate()" property in my code, but it's not working as expected.
Here's the relevant snippet of my code:
using FazendaEletronica.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace FazendaEletronica.Infra.Data.EntitiesConfiguration
{
public class AnimalConfiguration : IEntityTypeConfiguration<Animal>
{
public void Configure(EntityTypeBuilder<Animal> builder)
{
builder.HasKey(t => t.Id);
builder.Property(p => p.Number).IsRequired();
builder.Property(p => p.SurName).HasMaxLength(100);
builder.Property(p => p.BirthDay).IsRequired().HasColumnType("Date");
builder.Property(p => p.CharAnimalSex).HasMaxLength(1).IsRequired();
builder.Property(p => p.AnimalStatus).IsRequired();
builder.Property(p => p.Active).IsRequired().HasColumnType("bit");
builder.Property(p => p.SaleDate).HasColumnType("Date");
builder.HasOne(a => a.AnimalDad).WithMany(a => a.AnimalsDad).HasForeignKey(a => a.AnimalDadId).IsRequired(false);
builder.HasOne(p => p.AnimalClass).WithMany(a => a.Animals).HasForeignKey(f => f.AnimalClassId).IsRequired();
builder.HasOne(p => p.AnimalPurpose).WithMany(a => a.Animals).HasForeignKey(f => f.AnimalPurposeId).IsRequired();
builder.HasOne(p => p.UserFarm).WithMany(p => p.Animals).HasForeignKey(f => f.UserFarmId).IsRequired();
builder.Property(p => p.ModifiedAt).ValueGeneratedOnAddOrUpdate();
builder.Property(p => p.CreatedAt).ValueGeneratedOnAdd();
}
}
}
Could someone help me understand why the "ValueGeneratedOnAddOrUpdate()" property is not automatically generating the values? Is there any other approach I can take to achieve this result?
he ValueGeneratedOnAddOrUpdate() method in Entity Framework Core is used to configure a property to have its value automatically generated when a new record is inserted or updated. However, the behavior you are experiencing might be due to the fact that ValueGeneratedOnAddOrUpdate() is not supported for all property types.
英文:
I'm having trouble making the "CreatedAt" and "ModifiedAt" fields automatically generated by Entity Framework when adding and updating records in a table. I've tried using the "ValueGeneratedOnAddOrUpdate()" property in my code, but it's not working as expected.
Here's the relevant snippet of my code:
using FazendaEletronica.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace FazendaEletronica.Infra.Data.EntitiesConfiguration
{
public class AnimalConfiguration : IEntityTypeConfiguration<Animal>
{
public void Configure(EntityTypeBuilder<Animal> builder)
{
builder.HasKey(t => t.Id);
builder.Property(p => p.Number).IsRequired();
builder.Property(p => p.SurName).HasMaxLength(100);
builder.Property(p => p.BirthDay).IsRequired().HasColumnType("Date");
builder.Property(p => p.CharAnimalSex).HasMaxLength(1).IsRequired();
builder.Property(p => p.AnimalStatus).IsRequired();
builder.Property(p => p.Active).IsRequired().HasColumnType("bit");
builder.Property(p => p.SaleDate).HasColumnType("Date");
builder.HasOne(a => a.AnimalDad).WithMany(a => a.AnimalsDad).HasForeignKey(a => a.AnimalDadId).IsRequired(false);
builder.HasOne(p => p.AnimalClass).WithMany(a => a.Animals).HasForeignKey(f => f.AnimalClassId).IsRequired();
builder.HasOne(p => p.AnimalPurpose).WithMany(a => a.Animals).HasForeignKey(f => f.AnimalPurposeId).IsRequired();
builder.HasOne(p => p.UserFarm).WithMany(p => p.Animals).HasForeignKey(f => f.UserFarmId).IsRequired();
builder.Property(p => p.ModifiedAt).ValueGeneratedOnAddOrUpdate();
builder.Property(p => p.CreatedAt).ValueGeneratedOnAdd();
}
}
}
Could someone help me understand why the "ValueGeneratedOnAddOrUpdate()" property is not automatically generating the values? Is there any other approach I can take to achieve this result?
he ValueGeneratedOnAddOrUpdate() method in Entity Framework Core is used to configure a property to have its value automatically generated when a new record is inserted or updated. However, the behavior you are experiencing might be due to the fact that ValueGeneratedOnAddOrUpdate() is not supported for all property types.
答案1
得分: 0
一个选项是在 DataContext.OnSaveChanges
中处理它,使用基类来识别要自动处理的实体:
public abstract class EditableEntityBase
{
[DateTimeKind(DateTimeKind.Local), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedAt { get; set; }
[DateTimeKind(DateTimeKind.Local), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime LastModAt { get; set; }
}
/// <summary>
/// 用于保存更改的实现覆盖。这确保了记录了CreatedBy/At和ModifiedBy/At。
/// </summary>
public override int SaveChanges()
{
// 可选,如果您想要一个软删除系统(例如,IsActive=false而不是Delete...
// 这些DbContexts不允许硬删除。
if (ChangeTracker.Entries().Any(x => x.State == EntityState.Deleted
&& typeof(EditableEntityBase).IsAssignableFrom(x.GetType())))
throw new ApplicationException("不支持硬删除。");
var updatedEntities = ChangeTracker.Entries()
.Where(x => x.State == EntityState.Modified)
.Select(x => x.Entity)
.Cast<EditableEntityBase>();
foreach (var entity in updatedEntities)
{
entity.LastModAt = DateTime.Now;
}
var insertedEntities = ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added)
.Select(x => x.Entity)
.Cast<EditableEntityBase>();
foreach (var entity in insertedEntities)
{
entity.CreatedAt = entity.LastModAt = DateTime.Now;
}
return base.SaveChanges();
}
希望这对你有所帮助!
英文:
One option is to handle it in the DataContext.OnSaveChanges
using a base class to identify which entities you want to automatically handle:
public abstract class EditableEntityBase
{
[DateTimeKind(DateTimeKind.Local), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime CreatedAt { get; set; }
[DateTimeKind(DateTimeKind.Local), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime LastModAt { get; set; }
}
/// <summary>
/// Implementation override for saving changes. This ensures that CreatedBy/At
/// and ModifiedBy/At are recorded.
/// </summary>
public override int SaveChanges()
{
// Optional if you want a soft-delete system (I.e. IsActive=false rather than Delete...
// These DbContexts do not allow for hard Deletes.
if (ChangeTracker.Entries().Any(x => x.State == EntityState.Deleted
&& typeof(EditableEntityBase).IsAssignableFrom(x.GetType())))
throw new ApplicationException("Hard deletes are not supported.");
var updatedEntities = ChangeTracker.Entries()
.Where(x => x.State == EntityState.Modified)
.Select(x => x.Entity)
.Cast<EditableEntityBase>();
foreach (var entity in updatedEntities)
{
entity.LastModAt = DateTime.Now;
}
var insertedEntities = ChangeTracker.Entries()
.Where(x => x.State == EntityState.Added)
.Select(x => x.Entity)
.Cast<EditableEntityBase>();
foreach (var entity in insertedEntities)
{
entity.CreatedAt = entity.LastModAt = DateTime.Now;
}
return base.SaveChanges();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论