本文整理汇总了C#中DbEntityEntry.GetDatabaseValues方法的典型用法代码示例。如果您正苦于以下问题:C# DbEntityEntry.GetDatabaseValues方法的具体用法?C# DbEntityEntry.GetDatabaseValues怎么用?C# DbEntityEntry.GetDatabaseValues使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbEntityEntry
的用法示例。
在下文中一共展示了DbEntityEntry.GetDatabaseValues方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPrimaryKeyValuesOf
private static object GetPrimaryKeyValuesOf(
DbEntityEntry dbEntry,
List<PropertyConfiguerationKey> properties)
{
if (properties.Count == 1)
{
return dbEntry.GetDatabaseValues().GetValue<object>(properties.Select(x => x.PropertyName).First());
}
if (properties.Count > 1)
{
string output = "[";
output += string.Join(",",
properties.Select(colName => dbEntry.GetDatabaseValues().GetValue<object>(colName.PropertyName)));
output += "]";
return output;
}
throw new KeyNotFoundException("key not found for " + dbEntry.Entity.GetType().FullName);
}
示例2: LoggableEntity
public LoggableEntity(DbEntityEntry<BaseModel> entry)
{
DbPropertyValues originalValues =
(entry.State == EntityState.Modified || entry.State == EntityState.Deleted)
? entry.GetDatabaseValues()
: entry.CurrentValues;
Type entityType = entry.Entity.GetType();
if (entityType.Namespace == "System.Data.Entity.DynamicProxies") entityType = entityType.BaseType;
Properties = originalValues.PropertyNames.Select(name => new LoggableProperty(entry.Property(name), originalValues[name]));
Properties = entry.State == EntityState.Modified ? Properties.Where(property => property.IsModified) : Properties;
Properties = Properties.ToArray();
Action = entry.State.ToString();
Name = entityType.Name;
Id = entry.Entity.Id;
}
示例3: GetAuditLogs
private static IEnumerable<AuditLog> GetAuditLogs(DbEntityEntry entityEntry, string userName, EntityState entityState, ObjectContext objectContext,
DateTime auditDateTime)
{
var returnValue = new List<AuditLog>();
var keyRepresentation = BuildKeyRepresentation(entityEntry, KeySeperator, objectContext);
var auditedPropertyNames = GetDeclaredPropertyNames(entityEntry.Entity.GetType(), objectContext.MetadataWorkspace);
foreach (var propertyName in auditedPropertyNames)
{
var currentValue = Convert.ToString(entityEntry.CurrentValues.GetValue<object>(propertyName));
var originalValue = Convert.ToString(entityEntry.GetDatabaseValues().GetValue<object>(propertyName));
if (entityState == EntityState.Modified)
if (originalValue == currentValue) //Values are the same, don't log
continue;
returnValue.Add(new AuditLog
{
KeyNames = keyRepresentation.Key,
KeyValues = keyRepresentation.Value,
OriginalValue = entityState != EntityState.Added
? originalValue
: null,
NewValue = entityState == EntityState.Modified || entityState == EntityState.Added
? currentValue
: null,
ColumnName = propertyName,
EventDateTime = auditDateTime,
EventType = entityState.ToString(),
UserName = userName,
TableName = entityEntry.Entity.GetType().Name
});
}
return returnValue;
}
示例4: GetAuditRecordsForChange
private static IEnumerable<Entities.Log> GetAuditRecordsForChange(DbEntityEntry dbEntry, string userId)
{
List<Entities.Log> result = new List<Entities.Log>();
// Get the Table() attribute, if one exists
var tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(TableAttribute), false).SingleOrDefault() as TableAttribute;
// Get table name (if it has a Table attribute, use that, otherwise get the pluralized name)
var tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name;
if (dbEntry.State == EntityState.Added)
{
// For Inserts, just add the whole record
// If the entity implements IDescribableEntity, use the description from Describe(), otherwise use ToString()
result.Add(new Entities.Log(dbEntry)
{
//LogID = Guid.NewGuid(),
EventType = "A", // Added
TableName = tableName,
//RecordID = dbEntry.CurrentValues.GetValue<object>(keyName).ToString(), // Again, adjust this if you have a multi-column key
//RecordObj = dbEntry,
//ColumnName = "*ALL", // Or make it nullable, whatever you want
//NewValue = (dbEntry.CurrentValues.ToObject() is IDescribableEntity) ? (dbEntry.CurrentValues.ToObject() as IDescribableEntity).Describe() : dbEntry.CurrentValues.ToObject().ToString(),
Created_by = userId
//,Created_date = changeTime
});
}
else if (dbEntry.State == EntityState.Deleted)
{
var props = dbEntry.Entity.GetType().GetProperties();
var keyName = props.Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Any()).Name;
// Same with deletes, do the whole record, and use either the description from Describe() or ToString()
result.Add(new Entities.Log(dbEntry)
{
//LogID = Guid.NewGuid(),
EventType = "D", // Deleted
TableName = tableName,
RecordID = dbEntry.OriginalValues.GetValue<object>(keyName).ToString(),
//RecordObj = dbEntry,
//ColumnName = "*ALL",
//NewValue = (dbEntry.OriginalValues.ToObject() is IDescribableEntity) ? (dbEntry.OriginalValues.ToObject() as IDescribableEntity).Describe() : dbEntry.OriginalValues.ToObject().ToString(),
Created_by = userId
});
}
else if (dbEntry.State == EntityState.Modified)
{
var originalEntry = dbEntry.GetDatabaseValues();
foreach (var propertyName in dbEntry.OriginalValues.PropertyNames)
{
// For updates, we only want to capture the columns that actually changed
if (!Equals(originalEntry.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)))
{
var props = dbEntry.Entity.GetType().GetProperties();
var keyName = props.Single(p => p.GetCustomAttributes(typeof(KeyAttribute), false).Any()).Name;
result.Add(new Entities.Log()
{
EventType = "M",
TableName = tableName,
RecordID = dbEntry.OriginalValues.GetValue<object>(keyName).ToString(),
ColumnName = propertyName,
OriginalValue = originalEntry.GetValue<object>(propertyName).ToString(),
NewValue = dbEntry.CurrentValues.GetValue<object>(propertyName).ToString(),
Created_by = userId
});
}
}
}
// Otherwise, don't do anything, we don't care about Unchanged or Detached entities
return result;
}