In my DbContext, I have an override of the SaveChanges method to generate a Guid for entities to be added to the Database. This works fine except in the case of a shared primary key.
It generates different Id instead of one to be shared by the entities. Is there a way to check the type of Relationship?
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries().Where(e => e.State == EntityState.Added))
{
var t = entry.Entity.GetType();
if (t.GetProperty("Id") == null)
continue;
var info = t.GetProperty("Id").GetCustomAttributes(typeof (DatabaseGeneratedAttribute), true).Cast<DatabaseGeneratedAttribute>();
if (!info.Any() || info.Single().DatabaseGeneratedOption != DatabaseGeneratedOption.Identity)
{
if (t.GetProperty("Id").PropertyType == typeof(Guid) && (Guid)t.GetProperty("Id").GetValue(entry.Entity, null) == Guid.Empty)
t.GetProperty("Id").SetValue(entry.Entity, Guid.NewGuid(), null);
}
}
return base.SaveChanges();
}
Related
I'm trying to validate an entity coming from an External context has not changed.
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
I have a method which takes in an entity which has not been loaded from the context.
public bool Validate(Employee employee)
{
using (var context = new Context())
{
return context.Entry(employee).State == EntityState.Modified;
}
}
I would like to attach and verify that the attached entity is not modified from whats in the database.
I would prefer not to manually have to iterate of the properties. Is there a way to hack around this?
No need to attach the external entity. You can use the external entity to set values of the database entity and then check the state of the latter:
public bool Validate(Employee externalEmployee)
{
using var context = new Context(); // C# 8.0
var dbEntity = context.Where(x => x.Id == externalEmployee.Id).SingleOrDefault();
if (dbEntity != null)
{
context.Entry(dbEntity).CurrentValues.SetValues(externalEmployee);
return context.Entry(dbEntity).State == EntityState.Modified;
}
return false; // Or true, depending on your semantics.
}
You can try:
public static List<string> GetChanges<T>(this T obj, T dbObj)
{
List<string> result = new List<string>();
var type = typeof(T);
foreach (var prop in type.GetProperties())
{
var newValue = prop.GetValue(obj, null);
var dbValue = prop.GetValue(dbObj, null);
if(newValue == null && dbValue != null)
{
result.Add(prop.Name);
continue;
}
if (newValue != null && dbValue == null)
{
result.Add(prop.Name);
continue;
}
if (newValue == null && dbValue == null)
continue;
if (!newValue.ToString().Equals(dbValue.ToString()))
result.Add(prop.Name);
}
return result;
}
if resultList.Count > 0, your object has changes.
In your Validate Method:
public bool Validate(Employee employee)
{
using (var context = new Context())
{
Employee dbEmployee = context.Employee.Find(employee.Id);
if(employee.GetChanges(dbEmployee).Count > 0)
return true;
return false;
}
}
It's a god workaround =D
Works for me!
I have Net core API configured with .net core 2.0 and EF core 2.0. it contains repository pattern architecture.
Now, I am trying to implement Audit log for each save change using EF change tracker.
My Issue : Whenever I tries to add a log for edit/modification endpoint, the original value and current value remain same and it's newly updated value. so in that way I am not able to track the modification or a change.
Here is my ApplicationContext file where I have overridden save call.
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions options) : base(options: options) { }
public DbSet<Item> Item { get; set; }
public DbSet<ChangeLog> ChangeLog { get; set; }
public override int SaveChanges()
{
var modifiedEntities = ChangeTracker.Entries();
foreach (var change in modifiedEntities)
{
var entityType = change.Entity.GetType().Name;
if (entityType == "LogItem")
continue;
if (change.State == EntityState.Modified)
{
foreach (var prop in change.OriginalValues.Properties)
{
var id = change.CurrentValues["Id"].ToString();
//here both originalValue and currentValue are same and it's newly updated value
var originalValue = change.OriginalValues[prop]?.ToString();
var currentValue = change.CurrentValues[prop]?.ToString();
if (originalValue != currentValue)
{
ChangeLog.Add(
new ChangeLog()
{
CreationDateTime = DateTime.Now,
CreationUserId = 1,
Log = $"Edited item named {prop.Name} in {entityType} Id {id}.",
OldValue = originalValue,
NewValue = currentValue,
TableName = entityType,
FieldName = prop.Name
}
);
}
}
}
}
return base.SaveChanges();
}
}
Here is my base repository.
public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IFullAuditedEntity, new()
{
private readonly ApplicationContext context;
public EntityBaseRepository(ApplicationContext context)
{
this.context = context;
}
public virtual T GetSingle(int id) => context.Set<T>().AsNoTracking().FirstOrDefault(x => x.Id == id);
public virtual T Add(T entity) => Operations(entity: entity, state: EntityState.Added);
public virtual T Update(T entity) => Operations(entity: entity, state: EntityState.Modified);
public virtual T Delete(T entity) => Operations(entity: entity, state: EntityState.Deleted);
public virtual T Operations(T entity, EntityState state)
{
EntityEntry dbEntityEntry = context.Entry<T>(entity);
if (state == EntityState.Added)
{
entity.CreationDateTime = DateTime.UtcNow;
entity.CreationUserId = 1;
context.Set<T>().Add(entity);
dbEntityEntry.State = EntityState.Added;
}
else if (state == EntityState.Modified)
{
entity.LastModificationDateTime = DateTime.UtcNow;
entity.LastModificationUserId = 1;
//var local = context.Set<T>().Local.FirstOrDefault(entry => entry.Id.Equals(entity.Id));
//if (local != null)
//{
// context.Entry(local).State = EntityState.Detached;
//}
dbEntityEntry.State = EntityState.Modified;
}
else if (state == EntityState.Deleted)
{
entity.DeletionFlag = true;
entity.DeletionUserId = 1;
entity.DeletionDateTime = DateTime.UtcNow;
dbEntityEntry.State = EntityState.Modified;
}
return entity;
}
public virtual void Commit() => context.SaveChanges();
}
And Lastly my controller with end point for put.
[Produces("application/json")]
[Route("api/Item")]
public class ItemController : Controller
{
private readonly IItemRepository repository;
private readonly IChangeLogRepository changeLogRepository;
private readonly IMapper mapper;
public ItemController(IItemRepository repository, IChangeLogRepository _changeLogRepository, IMapper mapper)
{
this.repository = repository;
this.changeLogRepository = _changeLogRepository;
this.mapper = mapper;
}
[HttpPut]
public IActionResult Put([FromBody]ItemDto transactionItemDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (transactionItemDto.Id <= 0)
{
return new NotFoundResult();
}
Item item = repository.GetSingle(transactionItemDto.Id); //find entity first
if (item == null)
{
return new NotFoundResult();
}
//map all the properties and commit
var entity = mapper.Map<Item>(transactionItemDto);
var updatedItem = repository.Update(entity);
repository.Commit();
return new OkObjectResult(mapper.Map<Item, ItemDto>(source: updatedItem));
}
}
I am not sure where I am doing any mistake, I tried to check this case in SO, but no luck.
any help will be appreciated, thanks.
I am not using the repository pattern, but I have implemented a very similar audit log for EF Core 2.1. I loop through the list of changes that are being tracked by the entity framework change tracker, and log them.
What I have noticed is that when I want to update an entity, there are two ways to do it. One is that I read the existing entity from the database, reassign the variable, then save it. The second way is to simply create an object, attach it to the database context, and set the property I want to update to the modified state. When I do this, my auditing won't work for the original value, since the original value was never actually read from the database.
examples:
//auditing works fine
var myEntity = await db.MyEntity.FindAsync(entityId);
myEntity.Property = newValue;
await db.SaveChangesAsync();
//auditing can't track the old value
var myEntity = new MyEntity();
db.Attach(myEntity);
myEntity.Property = newValue;
await db.SaveChangesAsync();
Here is the important bit of my auditing code for example
foreach (var entity in db.ChangeTracker.Entries())
{
if(entity.State == EntityState.Detached || entity.State == EntityState.Unchanged)
{
continue;
}
var audits = new List<Audit>();
//the typeId is a string representing the primary keys of this entity.
//this will not be available for ADDED entities with generated primary keys, so we need to update those later
string typeId;
if (entity.State == EntityState.Added && entity.Properties.Any(prop => prop.Metadata.IsPrimaryKey() && prop.IsTemporary))
{
typeId = null;
}
else
{
var primaryKey = entity.Metadata.FindPrimaryKey();
typeId = string.Join(',', primaryKey.Properties.Select(prop => prop.PropertyInfo.GetValue(entity.Entity)));
}
//record an audit for each property of each entity that has been changed
foreach (var prop in entity.Properties)
{
//don't audit anything about primary keys (those can't change, and are already in the typeId)
if(prop.Metadata.IsPrimaryKey() && entity.Properties.Any(p => !p.Metadata.IsPrimaryKey()))
{
continue;
}
//ignore values that won't actually be written
if(entity.State != EntityState.Deleted && entity.State != EntityState.Added && prop.Metadata.AfterSaveBehavior != PropertySaveBehavior.Save)
{
continue;
}
//ignore values that won't actually be written
if (entity.State == EntityState.Added && prop.Metadata.BeforeSaveBehavior != PropertySaveBehavior.Save)
{
continue;
}
//ignore properties that didn't change
if(entity.State == EntityState.Modified && !prop.IsModified)
{
continue;
}
var audit = new Audit
{
Action = (int)entity.State,
TypeId = typeId,
ColumnName = prop.Metadata.SqlServer().ColumnName,
OldValue = (entity.State == EntityState.Added || entity.OriginalValues == null) ? null : JsonConvert.SerializeObject(prop.OriginalValue),
NewValue = entity.State == EntityState.Deleted ? null : JsonConvert.SerializeObject(prop.CurrentValue)
};
}
//Do something with audits
}
I think I see the issue with your code. In your controller:
//map all the properties and commit
var entity = mapper.Map<Item>(transactionItemDto);
var updatedItem = repository.Update(entity);
repository.Commit();
In that code you are taking your DTO and mapping it to a new instance of Item. That new instance of Item knows nothing of the current database values, that is why you are seeing the same new values for both OriginalValue and CurrentValue.
If you reuse the Item item variable that you get in this line:
Item item = repository.GetSingle(transactionItemDto.Id); //find entity first
Note, you'll need to get the entity with tracking on however, vs how your repository GetSingle does it with AsNoTracking. If you use that item (which now has the original/current database values) and map your transactionItemDto properties onto it like this:
var entityToUpdate = mapper.Map<ItemDto, Item>(transactionItemDto);
Then, when you call your repository.Update method passing it entityToUpdate, I believe you'll see the correct before/after values.
.
.
.
.
Old (wrong) answer I originally posted:
In your ApplicationContext code you have the following loop
foreach (var prop in change.OriginalValues.Properties)
I believe that is what is causing your original value/current values to be the same because you are looping over the original values properties. Try changing that loop to:
foreach (var prop in change.Properties)
Then, try reading the values off each property via the prop variable like so:
var currentValue = prop.CurrentValue;
var originalValue = prop.OriginalValue;
EDIT: Ah - I see now that in your code you are trying to read the original value from the change.OriginalValues collection, so I don't think this is going to help.
there are two approaches for this:
var entry = _dbContext.Attach(entity);
var updated = entry.CurrentValues.Clone();
entry.Reload();
entry.CurrentValues.SetValues(updated);
entry.State = EntityState.Modified;
db.SaveChanges();
Or Simply do:
var persondb = db.Persons.Find(person.id);
db.Entry(persondb).CurrentValues.SetValues(person);
db.SaveChanges();
I am porting an old project over to ASP.NET 5 and Entity Framework 7. I have used the database first approach (DNX scaffold) to create the model.
The old project is based on Entity Framework 4 and audit tracking is implemented by overriding the SaveChanges method of the DbContext:
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
int? UserId = null;
if (System.Web.HttpContext.Current != null)
UserId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault();
foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
{
Type EntityType = entry.Entity.GetType();
PropertyInfo pCreated = EntityType.GetProperty("Created");
PropertyInfo pCreatedById = EntityType.GetProperty("CreatedById");
PropertyInfo pModified = EntityType.GetProperty("Modified");
PropertyInfo pModifiedById = EntityType.GetProperty("ModifiedById");
if (entry.State == EntityState.Added)
{
if (pCreated != null)
pCreated.SetValue(entry.Entity, DateTime.Now, new object[0]);
if (pCreatedById != null && UserId != null)
pCreatedById.SetValue(entry.Entity, UserId, new object[0]);
}
if (pModified != null)
pModified.SetValue(entry.Entity, DateTime.Now, new object[0]);
if (pModifiedById != null && UserId != null)
pModifiedById.SetValue(entry.Entity, UserId, new object[0]);
}
}
return base.SaveChanges(options);
}
My question is, how can I implement this in Entity Framework 7? Do I have to take the code first approach?
Basically you have two ways to achieve this:
Using ChangeTracker API (EF 6+):
This is the way we currently do it in EF 6 and it is still valid and working for EF 7:
First you have to make sure your entities are implementing a common interface for audit fields:
public interface IAuditableEntity
{
int? CreatedById { get; set; }
DateTime Created { get; set; }
int? ModifiedById { get; set; }
DateTime Modified { get; set; }
}
Then you can override SaveChanges and update each common field with audit values:
public override int SaveChanges()
{
int? userId = null;
if (System.Web.HttpContext.Current != null)
userId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault();
var modifiedEntries = ChangeTracker.Entries<IAuditableEntity>()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);
foreach (EntityEntry<IAuditableEntity> entry in modifiedEntries)
{
entry.Entity.ModifiedById = UserId;
entry.Entity.Modified = DateTime.Now;
if (entry.State == EntityState.Added)
{
entry.Entity.CreatedById = UserId;
entry.Entity.Created = DateTime.Now;
}
}
return base.SaveChanges();
}
Using EF 7 new "Shadow Properties" Feature:
Shadow properties are properties that do not exist in your entity class. The value and state of these properties is maintained purely in the Change Tracker.
In other words, the audit columns will not be exposed on your entities which seems to be a better option compare to the one above where you have to include them on your entities.
To implement shadow properties, first you have to configure them on your entities. Let's say for example you have a User object that needs to have some audit columns:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().Property<int>("CreatedById");
modelBuilder.Entity<User>().Property<DateTime>("Created");
modelBuilder.Entity<User>().Property<int>("ModifiedById");
modelBuilder.Entity<User>().Property<DateTime>("Modified");
}
Once configured, now you can access them on SaveChanges() override and update their values accordingly:
public override int SaveChanges()
{
int? userId = null;
if (System.Web.HttpContext.Current != null)
userId = (from user in Users.Where(u => u.UserName == System.Web.HttpContext.Current.User.Identity.Name) select user.Id).SingleOrDefault();
var modifiedBidEntries = ChangeTracker.Entries<User>()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);
foreach (EntityEntry<User> entry in modifiedBidEntries)
{
entry.Property("Modified").CurrentValue = DateTime.UtcNow;
entry.Property("ModifiedById").CurrentValue = userId;
if (entry.State == EntityState.Added)
{
entry.Property("Created").CurrentValue = DateTime.UtcNow;
entry.Property("CreatedById").CurrentValue = userId;
}
}
return base.SaveChanges();
}
Final Thoughts:
For implementing something like audit columns, I'll take the Shadow Properties approach since these are cross cutting concerns and do not necessarily belong to my domain objects so having them implemented this way will keep my domain objects nice and clean.
I've worked on a library that might help.
Take a look at Audit.EntityFramework library, it intercepts SaveChanges() and is compatible with EF Core versions.
I'm having trouble getting the Id of an object passed by parameter.
The code:
private void SetEntityState(TEntity entity, EntityState entityStated)
{
var entry = this.Context.Entry(entity);
if (entry.State == EntityState.Detached)
{
// The problem is here.
TEntity attachedEntity = this.DbSet.Where(
x => x.Id.Equals(entity.Id)
).FirstOrDefault();
if (attachedEntity != null)
{
var attachedEntry = this.Context.Entry(attachedEntity);
attachedEntry.CurrentValues.SetValues(entity);
attachedEntry.State = entityStated;
}
else
{
entry.State = entityStated;
}
}
}
The problem is here:
TEntity attachedEntity = this.DbSet.Where(
x => x.Id.Equals(entity.Id)
).FirstOrDefault();
The property 'Id' of the 'entity' is always returning 0 but in debbug, I can see that the value is 3.
Why is this happening?
Maybe you can try (for testing):
var entry = Context.Find(entity);
instead of
var entry = this.Context.Entry(entity);
Try this
TEntity attachedEntity = this.DbSet.Where(x => x.Id == entity.Id).FirstOrDefault();
OR
Int32 entityId = entity.Id;
TEntity attachedEntity = this.DbSet.Where(x => x.Id == entityId).FirstOrDefault();
In NHibernate we override Equals() and GetHashCode() to calculate entity equality.
Is the same necessary in Entity Framework?
The reason I ask is we are using EF4 with POCOs and are in the process of implementing a caching layer. The problem we have is when checking to see if an item is already attached to the object context. Currently this evaluates to false, even when the entity already exists in the current object context.
Maybe this code will give you some ideas. Im not using POCOs, but common points are the same I think.
Here is sample Update method which checks if Context has entity attached before performing updating routine.
public T Update(T entity)
{
if (entity == null) throw new ArgumentNullException("entity");
var key = ObjectContext.CreateEntityKey(ObjectContext.GetEntitySet<T>().Name, entity);
if (ObjectContext.IsAttached(key))
{
ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
}
else
{
ObjectContext.AttachTo(ObjectContext.GetEntitySet<T>().Name, entity);
ObjectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
}
return entity;
}
internal static EntitySetBase GetEntitySet<TEntity>(this ObjectContext context)
{
var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
var baseType = GetBaseType(typeof(TEntity));
var entitySet = container.BaseEntitySets
.Where(item => item.ElementType.Name.Equals(baseType.Name))
.FirstOrDefault();
return entitySet;
}
internal static bool IsAttached(this ObjectContext context, EntityKey key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
ObjectStateEntry entry;
if (context.ObjectStateManager.TryGetObjectStateEntry(key, out entry))
{
return (entry.State != EntityState.Detached);
}
return false;
}
private static Type GetBaseType(Type type)
{
var baseType = type.BaseType;
if (baseType != null && baseType != typeof(EntityObject))
{
return GetBaseType(type.BaseType);
}
return type;
}
Hope this can help you a bit=)