本文整理汇总了C#中DbEntityEntry类的典型用法代码示例。如果您正苦于以下问题:C# DbEntityEntry类的具体用法?C# DbEntityEntry怎么用?C# DbEntityEntry使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DbEntityEntry类属于命名空间,在下文中一共展示了DbEntityEntry类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRecordsForChange
public static string GetRecordsForChange(DbEntityEntry dbEntry)
{
var returnvalue = "";
var changeTime = DateTime.UtcNow;
switch (dbEntry.State)
{
case System.Data.Entity.EntityState.Added:
returnvalue = string.Format("Add:{0};Value:{1} ", dbEntry.CurrentValues, dbEntry.OriginalValues);
break;
case System.Data.Entity.EntityState.Modified:
foreach (string propertyName in dbEntry.OriginalValues.PropertyNames)
{
// For updates, we only want to capture the columns that actually changed
if (!object.Equals(dbEntry.OriginalValues.GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)))
{
returnvalue += string.Format("Modified:{0};OriginalValue:{1};NewValue:{2}|", propertyName, dbEntry.CurrentValues.GetValue<object>(propertyName), dbEntry.OriginalValues.GetValue<object>(propertyName));
}
}
break;
}
return returnvalue;
}
示例2: ValidateEntity
/// <summary>
/// <see cref="DbContext.ValidateEntity"/>
/// </summary>
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
IDictionary<object, object> items)
{
if(entityEntry != null && entityEntry.State == EntityState.Added)
{
// Validation for duplicate email
UserBase user = entityEntry.Entity as UserBase;
if(user != null)
{
if(Users.Any(u => u.Email.ToUpper() == user.Email.ToUpper()))
{
return new DbEntityValidationResult(entityEntry, new List<DbValidationError>())
{
ValidationErrors =
{
new DbValidationError("User",
string.Format(CultureInfo.CurrentCulture, "Duplicate Email. Email: {0}", user.Email))
}
};
}
}
}
return base.ValidateEntity(entityEntry, items);
}
示例3: TrySetVersion
private static void TrySetVersion(DbEntityEntry entry, Func<int, int> versionSet)
{
var v = entry.TryGetProperty("Version");
if (v != null && v.CurrentValue is int) {
v.CurrentValue = versionSet((int)v.CurrentValue);
}
}
示例4: Remove
public void Remove(DbEntityEntry entry)
{
var id = GetId(entry.Entity);
var existing = Data.FirstOrDefault(x => x.Key.Equals(id));
Data.Remove(existing);
}
示例5: ValidateEntity
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry,
IDictionary<object, object> items)
{
if (entityEntry.State != EntityState.Added)
{
return base.ValidateEntity(entityEntry,
items);
}
var user = entityEntry.Entity as User;
// Check for uniqueness of user name
if (user == null || !Users.Any(u => String.Equals(u.UserName,
user.UserName,
StringComparison.CurrentCultureIgnoreCase)))
{
return base.ValidateEntity(entityEntry,
items);
}
var result = new DbEntityValidationResult(entityEntry,
new List<DbValidationError>());
result.ValidationErrors.Add(new DbValidationError("User",
"User name must be unique."));
return result;
}
示例6: DbEntityEntryWrapper
public DbEntityEntryWrapper(DbEntityEntry entity)
{
if (entity == null)
throw new ArgumentNullException(nameof(entity));
EntityEntry = entity;
}
示例7: GetEntityKeyDeleted
/// <summary>
/// Gets the entity key deleted.
/// </summary>
/// <param name="entry">The entry.</param>
/// <param name="entitySetName">Name of the entity set.</param>
/// <returns></returns>
private string GetEntityKeyDeleted(DbEntityEntry entry, out string entitySetName)
{
var keyBuilder = new StringBuilder();
var keys = entry.Entity.GetType().GetProperties().Where(w => w.GetCustomAttributes(typeof(KeyAttribute), true).Any()).Select(p => p.Name).ToArray();
entitySetName = ResolveEntitySetName(entry.Entity.GetType());
foreach (var key in keys)
{
var objectVal = entry.Property(key).CurrentValue;
if (objectVal == null)
{
continue;
}
var keyValue = objectVal.ToString();
if (keyBuilder.Length > 0)
{
keyBuilder.Append(",");
}
keyBuilder.Append(keyValue);
}
return keyBuilder.ToString();
}
示例8: 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;
}
示例9: ValidateEntity
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
var result = new DbEntityValidationResult(entityEntry, new List<DbValidationError>());
if (entityEntry.Entity is Application && entityEntry.State == EntityState.Added)
{
Application app = entityEntry.Entity as Application;
if (!app.IsValid)
{
foreach (ValidationError ve in app.GetValidationErrors())
{
result.ValidationErrors.Add(new DbValidationError(ve.PropertyName, ve.ErrorMessage));
}
}
}
if (result.ValidationErrors.Count > 0)
{
return result;
}
else
{
return base.ValidateEntity(entityEntry, items);
}
}
示例10: ValidateEntity
public static void ValidateEntity(DbContext context, DbEntityEntry entity, Type type)
{
if (entity.State == System.Data.EntityState.Modified)
{
if (!_parentAttributes.ContainsKey(type))
{
var properties = from attributedProperty in type.GetProperties()
select new
{
attributedProperty,
attributes = attributedProperty.GetCustomAttributes(true)
.Where(attribute => attribute is ParentAttribute)
};
properties = properties.Where(p => p.attributes.Any());
_parentAttributes.Add(type,
properties.Any()
? properties.First().attributedProperty.Name
: string.Empty);
}
if (!string.IsNullOrEmpty(_parentAttributes[type]))
{
if (entity.Reference(_parentAttributes[type]).CurrentValue == null)
{
context.Set(type).Remove(entity.Entity);
}
}
}
}
示例11: DbEntityValidationResult
/// <summary>
/// Creates an instance of <see cref="DbEntityValidationResult" /> class.
/// </summary>
/// <param name="entry"> Entity entry the results applies to. Never null. </param>
/// <param name="validationErrors">
/// List of <see cref="DbValidationError" /> instances. Never null. Can be empty meaning the entity is valid.
/// </param>
public DbEntityValidationResult(DbEntityEntry entry, IEnumerable<DbValidationError> validationErrors)
{
Check.NotNull(entry, "entry");
Check.NotNull(validationErrors, "validationErrors");
_entry = entry.InternalEntry;
_validationErrors = validationErrors.ToList();
}
示例12: GetTypeKey
public string GetTypeKey(DbEntityEntry entry)
{
var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
string type = string.Empty;
if (objectStateEntry.EntityKey.EntityKeyValues != null)
type = objectStateEntry.EntityKey.EntityKeyValues[0].Value.GetType().ToString();
return type;
}
示例13: DbEntityValidationResult
/// <summary>
/// Creates an instance of <see cref = "DbEntityValidationResult" /> class.
/// </summary>
/// <param name = "entry">
/// Entity entry the results applies to. Never null.
/// </param>
/// <param name = "validationErrors">
/// List of <see cref = "DbValidationError" /> instances. Never null. Can be empty meaning the entity is valid.
/// </param>
public DbEntityValidationResult(DbEntityEntry entry, IEnumerable<DbValidationError> validationErrors)
{
Contract.Requires(entry != null);
Contract.Requires(validationErrors != null);
_entry = entry.InternalEntry;
_validationErrors = validationErrors.ToList();
}
示例14: UpdateTrackedEntity
/// <summary>
/// Looks at everything that has changed and applies any further action if required.
/// </summary>
/// <param name="entityEntry"></param>
/// <returns></returns>
private static void UpdateTrackedEntity(DbEntityEntry entityEntry)
{
var trackUpdateClass = entityEntry.Entity as IModifiedEntity;
if (trackUpdateClass == null) return;
trackUpdateClass.ModifiedDate = DateTime.UtcNow;
if (entityEntry.State == EntityState.Added)
trackUpdateClass.rowguid = Guid.NewGuid();
}
示例15: AdjustTimestamps
protected virtual void AdjustTimestamps(DbEntityEntry<IPersistenceAudit> entity)
{
if (entity.State == EntityState.Added)
entity.Entity.CreatedTimestamp = entity.Entity.ModifiedTimestamp = DateTime.Now;
if (entity.State == EntityState.Modified)
entity.Entity.ModifiedTimestamp = DateTime.Now;
}