{
DateTime CreatedOn { get; set; }
DateTime? UpdatedOn { get; set; }
TId CreatedById { get; set; }
T CreatedBy { get; set; }
TId? UpdatedById { get; set; }
T? UpdatedBy { get; set; }
}
public abstract class AuditableEntity : IAuditable
{
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
public TId CreatedById { get; set; }
public T CreatedBy { get; set; }
public TId? UpdatedById { get; set; }
public T? UpdatedBy { get; set; }
}
public abstract class AuditableEntityTypeConfiguration : IEntityTypeConfiguration
where Th : AuditableEntity
where T : class
{
public virtual void Configure(EntityTypeBuilder builder)
{
builder.Property(e => e.CreatedOn).IsRequired();
builder.Property(e => e.UpdatedOn);
builder.HasOne(x => x.CreatedBy)
.WithMany()
.HasForeignKey(x => x.CreatedById)
.IsRequired();
builder.HasOne(x => x.UpdatedBy)
.WithMany()
.HasForeignKey(x => x.UpdatedById);
}
}
public class Paper : AuditableEntity
{
public Guid Id { get; set; }
...
}```
However, when I attempt to make a migration, it always returns changes making the previously nullable property UpdatedById to now be required.
I've tried adding this to my OnModelCreating in addition to implementing the abstract class as AuditableEntity in my DbContext:
```modelBuilder.Entity().Property(p => p.UpdatedById).IsRequired(false);
modelBuilder.Entity().Property(p => p.UpdatedById).IsRequired(false);
Upon running the migration, I am left with:
Unable to create a 'DbContext' of type '{MyDbContext}'. The exception 'The property 'Paper.UpdatedById' cannot be marked as nullable/optional because the type of the property is 'Guid' which is not a nullable type. Any property can be marked as non-nullable/required, but only properties of nullable types can be marked as nullable/optional.' was thrown while attempting to create an instance. For the different patterns supported at design time, see here.
Does anyone have any ideas on how to resolve this?
0 comments:
Post a Comment
Thanks