Skip to main content
added 2 characters in body
Source Link
Heslacher
  • 51k
  • 5
  • 83
  • 177

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called AlwaysUpdate which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as ModifiedModified. Is this code all right?

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries();
    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
        {
            foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                // Try catch removes the errors for detached and unchanged items
                try
                {
                    entry.Property(prop).IsModified = true;
                }
                catch { }
            }
        }
    }
    return base.SaveChanges();
}

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called AlwaysUpdate which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries();
    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
        {
            foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                // Try catch removes the errors for detached and unchanged items
                try
                {
                    entry.Property(prop).IsModified = true;
                }
                catch { }
            }
        }
    }
    return base.SaveChanges();
}

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called AlwaysUpdate which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries();
    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
        {
            foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                // Try catch removes the errors for detached and unchanged items
                try
                {
                    entry.Property(prop).IsModified = true;
                }
                catch { }
            }
        }
    }
    return base.SaveChanges();
}
deleted 82 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called 'AlwaysUpdate'AlwaysUpdate which I apply to the property. I then overrode SaveChangesSaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

    public override int SaveChanges()
    {
        var changeSet = ChangeTracker.Entries();
        if (changeSet != null)
        {
            foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
            {
                foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
                {
                    if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                    // Try catch removes the errors for detached and unchanged items
                    try
                    {
                        entry.Property(prop).IsModified = true;
                    }
                    catch { }
                }
            }
        }
        return base.SaveChanges();
    }

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called 'AlwaysUpdate' which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

    public override int SaveChanges()
    {
        var changeSet = ChangeTracker.Entries();
        if (changeSet != null)
        {
            foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
            {
                foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
                {
                    if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                    // Try catch removes the errors for detached and unchanged items
                    try
                    {
                        entry.Property(prop).IsModified = true;
                    }
                    catch { }
                }
            }
        }
        return base.SaveChanges();
    }

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called AlwaysUpdate which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries();
    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
        {
            foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
            {
                if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                // Try catch removes the errors for detached and unchanged items
                try
                {
                    entry.Property(prop).IsModified = true;
                }
                catch { }
            }
        }
    }
    return base.SaveChanges();
}
Tweeted twitter.com/#!/StackCodeReview/status/292991848224542720
Source Link
Charles
  • 153
  • 4

AlwaysUpdate attribute for Entity Framework Code First POCO

The entity framework will only save properties it detects has changed via its proxy classes. I have a situation where I want a property to always be saved no matter if it changed or not.

I wrote a blank attribute called 'AlwaysUpdate' which I apply to the property. I then overrode SaveChanges to scan for these attributes and mark the field as Modified. Is this code all right?

    public override int SaveChanges()
    {
        var changeSet = ChangeTracker.Entries();
        if (changeSet != null)
        {
            foreach (var entry in changeSet.Where( c=> c.State == System.Data.EntityState.Modified ))
            {
                foreach (var prop in entry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(AlwaysUpdateAttribute), true).Count() > 0).Select( p => p.Name ))
                {
                    if (entry.State == System.Data.EntityState.Added || entry.State == System.Data.EntityState.Unchanged) continue;
                    // Try catch removes the errors for detached and unchanged items
                    try
                    {
                        entry.Property(prop).IsModified = true;
                    }
                    catch { }
                }
            }
        }
        return base.SaveChanges();
    }