EntityFramework-Effort: DatabaseGeneratedOption.Computed always causes null

I have a column like the following:

    [Column, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime? LastModified { get; set; }

and a SQL trigger that sets it as expected. Entity framework expects the database to set this property and reads it back after each insert and update. Effort always reports this as null. That is:

    var entity = new MyEntity { LastModified = DateTime.Now };
    dbContext.MyEntities.Add(entity);
    dbContext.SaveChanges();
    Assert.NotNull(entity.LastModified);

fails where dbContext is a DbContext created from a transcient Effort DbConnection.

I would like some mechanism to get around this. Perhaps an EntityWithComputedColumnInserting event and an EntityWithComputedColumnUpdating event for allowing users to handle the computing of computed columns. Say using an EventArgs similar to the following:

    public class ComputedColumnEventArgs : EventArgs
    {
        public Type EntityType { get; }
        public string ComputedColumnName { get; }
        public object ComputedValue { get; set; }
    }

I could then make an event handler like the following in my test class:

    private void ComputeLastModified(object sender, ComputedColumnEventArgs e)
    {
        if (e.EntityType = typeof(MyEntity) 
            && e.ComputedColumnName == nameof(MyEntity.LastModified))
        {
            e.ComputedValue = _now;
        }
    }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 24 (11 by maintainers)

Most upvoted comments

Hello @manojItela ,

We recently started to work on this project (We want to clean most issues/pull in September), we will very soon look how to handle this scenario since that’s very common.

Best Regards,

Jonathan

I’m facing the same situation at the moment. I’m going to download the source code and try to find if it’s possible to implement the solution you are suggesting.