本文整理汇总了C#中DbEntityEntry.Property方法的典型用法代码示例。如果您正苦于以下问题:C# DbEntityEntry.Property方法的具体用法?C# DbEntityEntry.Property怎么用?C# DbEntityEntry.Property使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbEntityEntry
的用法示例。
在下文中一共展示了DbEntityEntry.Property方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Add
public void Add(DbEntityEntry entry)
{
SetId(entry.Entity);
var idName = GetIdPropertyInfo(entry.Entity.GetType()).Name;
entry.Property(idName).IsModified = false;
Data.Add(new KeyDataItem()
{
Entity = entry.Entity,
Key = GetId(entry.Entity)
});
}
示例2: Next
public void Next(DbEntityEntry entry)
{
var entity = entry.Entity;
var entityName = entity.GetType().Name;
var seqName = SeqName(entity);
var id = GetNextId(seqName);
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
var wKey =
objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets.First(meta => meta.ElementType.Name == entityName)
.ElementType.KeyMembers.Select(k => k.Name).FirstOrDefault();
entry.Property(wKey).CurrentValue = id;
}
示例3: 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;
}
示例4: LoggableEntity_CreatesPropertiesForAttachedEntity
public void LoggableEntity_CreatesPropertiesForAttachedEntity()
{
context.Dispose();
String title = model.Title;
context = new TestingContext();
context.Set<Role>().Attach(model);
entry = context.Entry<BaseModel>(model);
entry.OriginalValues["Title"] = "Role";
entry.CurrentValues["Title"] = "Role";
entry.State = EntityState.Modified;
IEnumerator<LoggableProperty> expected = new List<LoggableProperty> { new LoggableProperty(entry.Property("Title"), title) }.GetEnumerator();
IEnumerator<LoggableProperty> actual = new LoggableEntity(entry).Properties.GetEnumerator();
while (expected.MoveNext() | actual.MoveNext())
{
Assert.Equal(expected.Current.IsModified, actual.Current.IsModified);
Assert.Equal(expected.Current.ToString(), actual.Current.ToString());
}
}
示例5: GroupAttendedTransaction
/// <summary>
/// Initializes a new instance of the <see cref="GroupMemberChangeTransaction"/> class.
/// </summary>
/// <param name="entry">The entry.</param>
public GroupAttendedTransaction( DbEntityEntry entry )
{
if ( entry.State != EntityState.Deleted )
{
// Get the attendance record
var attendance = entry.Entity as Attendance;
// If attendance record is valid and the DidAttend is selected
if ( attendance != null && ( attendance.DidAttend ?? false ) )
{
// Save for all adds
bool valid = entry.State == EntityState.Added;
// If not an add, check previous DidAttend value
if ( !valid )
{
var dbProperty = entry.Property( "DidAttend" );
if ( dbProperty != null )
{
// Only use changes where DidAttend was previously not true
valid = !(dbProperty.OriginalValue as bool? ?? false);
}
}
if ( valid )
{
// Save the values
GroupId = attendance.GroupId;
PersonAliasId = attendance.PersonAliasId;
AttendanceDateTime = attendance.StartDateTime;
if ( attendance.Group != null )
{
GroupTypeId = attendance.Group.GroupTypeId;
}
}
}
}
}
示例6: SetValues
private static void SetValues(DbEntityEntry dbEntry, DataModificationEntry entry, Type entityType)
{
if (entry.IsFullReplaceUpdate)
{
// The algorithm for a "FullReplaceUpdate" is taken from ObjectContextServiceProvider.ResetResource
// in WCF DS, and works as follows:
// - Create a new, blank instance of the entity.
// - Copy over the key values and set any updated values from the client on the new instance.
// - Then apply all the properties of the new instance to the instance to be updated.
// This will set any unspecified properties to their default value.
object newInstance = Activator.CreateInstance(entityType);
SetValues(newInstance, entityType, entry.EntityKey);
SetValues(newInstance, entityType, entry.LocalValues);
dbEntry.CurrentValues.SetValues(newInstance);
}
else
{
foreach (KeyValuePair<string, object> propertyPair in entry.LocalValues)
{
DbPropertyEntry propertyEntry = dbEntry.Property(propertyPair.Key);
object value = propertyPair.Value;
if (propertyEntry is DbComplexPropertyEntry)
{
var dic = value as IReadOnlyDictionary<string, object>;
if (dic == null)
{
// TODO GitHubIssue#103 : Choose property error message for unknown type
throw new NotSupportedException("Unsupported type for property:" + propertyPair.Key);
}
var type = propertyEntry.CurrentValue.GetType();
value = Activator.CreateInstance(type);
SetValues(value, type, dic);
}
propertyEntry.CurrentValue = value;
}
}
}
示例7: ValidateEntity
protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
{
//HLA 申请单
if (entityEntry.Entity is HLA_shenqingdan)
{
if (entityEntry.State == EntityState.Modified)
{
if (entityEntry.Property("bianhao").IsModified)
{
var ls = entityEntry.Property("bianhao").OriginalValue as string;
if (!ls.Equals("临时编号")) throw new Exception("业务逻辑错误,编号一旦确定便不能修改!");
}
}
}
//HLA 样本接受
if (entityEntry.Entity is HLA_yangbenjieshou)
{
if (entityEntry.State == EntityState.Modified)
{
if (entityEntry.Property("bianhao").IsModified)
{
var ls = entityEntry.Property("bianhao").OriginalValue as string;
if (!ls.Equals("临时编号")) throw new Exception("业务逻辑错误,编号一旦确定便不能修改!");
}
if (entityEntry.Property("leixing").IsModified) throw new Exception("业务逻辑错误,样本类型一旦确定便不能修改!");
}
}
//HLA 布板说明
if (entityEntry.Entity is HLA_bubanshuoming)
{
if (entityEntry.State==EntityState.Modified)
{
var dangqian=(HLA_bubanshuoming)entityEntry.Entity;
if (!dangqian.HLA_hang.HLA_banxinxi.leixing.Equals("杂板") && !dangqian.HLA_hang.HLA_banxinxi.leixing.Equals(dangqian.HLA_weidian.leixing))
{
throw new Exception("非杂板,只能布相同类型的位点!");
}
}
}
//HLA 板信息
if (entityEntry.Entity is HLA_banxinxi)
{
if (entityEntry.State == EntityState.Modified)
{
if (entityEntry.Property("hangshu").IsModified) throw new Exception("业务逻辑错误,行数一旦确定便不能修改!");
if (entityEntry.Property("lieshu").IsModified) throw new Exception("业务逻辑错误,列数一旦确定便不能修改!");
if (entityEntry.Property("leixing").IsModified) throw new Exception("业务逻辑错误,类型一旦确定便不能修改!");
}
if (entityEntry.State == EntityState.Added)
{
var dangqian = (HLA_banxinxi)entityEntry.Entity;
if (dangqian.hangshu < 1) throw new Exception("业务逻辑错误,对于新加的,行数必须设定!");
if (dangqian.hangs.Count == 0) throw new Exception("业务逻辑错误,对于新加的,行必须设定!");
bool keyi = false;
if (dangqian.leixing.Equals("A")) keyi = true;
if (dangqian.leixing.Equals("B")) keyi = true;
if (dangqian.leixing.Equals("C")) keyi = true;
if (dangqian.leixing.Equals("DPB1")) keyi = true;
if (dangqian.leixing.Equals("DQB1")) keyi = true;
if (dangqian.leixing.Equals("DRB1")) keyi = true;
if (dangqian.leixing.Equals("杂板")) keyi = true;
if (keyi==false)
{
throw new Exception("类型超出范围!");
}
}
}
return base.ValidateEntity(entityEntry, items);
}
示例8: ValidateEntity
/// <summary>
/// Validates the entity.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="entity">The entity.</param>
/// <param name="type">The type.</param>
/// <returns>true if entity is removed</returns>
public static bool ValidateEntity(DbContext context, DbEntityEntry entity, Type type)
{
if (entity.State == EntityState.Modified)
{
if(IsRemovable(type))
{
foreach (var parentAttribute in _parentAttributes[type])
{
//Parent must have ForeignKey
if (String.IsNullOrWhiteSpace(parentAttribute.ForeignKey))
{
continue;
}
//Navigation property must be null
if (entity.Reference(parentAttribute.Name).CurrentValue != null)
{
continue;
}
var fkProperty = entity.Property(parentAttribute.ForeignKey);
//ForegnKey must be modified
if (!fkProperty.IsModified)
{
continue;
}
var isFkNullable = ReferenceEquals(fkProperty.CurrentValue, null);
if (!isFkNullable)
{
var fkType = fkProperty.CurrentValue.GetType();
isFkNullable = !fkType.IsValueType || Nullable.GetUnderlyingType(fkType) != null;
}
//ForeignKey must be null if type is nullable
//(Thats how EF work when removing item from collection)
if (isFkNullable && fkProperty.CurrentValue != null)
{
continue;
}
context.Set(type).Remove(entity.Entity);
return true;
}
}
}
return false;
}
示例9: SetValues
private void SetValues(DbEntityEntry dbEntry, DataModificationItem item, Type resourceType)
{
if (item.IsFullReplaceUpdateRequest)
{
// The algorithm for a "FullReplaceUpdate" is taken from ObjectContextServiceProvider.ResetResource
// in WCF DS, and works as follows:
// - Create a new, blank instance of the entity.
// - Copy over the key values and set any updated values from the client on the new instance.
// - Then apply all the properties of the new instance to the instance to be updated.
// This will set any unspecified properties to their default value.
object newInstance = Activator.CreateInstance(resourceType);
SetValues(newInstance, resourceType, item.ResourceKey);
SetValues(newInstance, resourceType, item.LocalValues);
dbEntry.CurrentValues.SetValues(newInstance);
}
else
{
foreach (KeyValuePair<string, object> propertyPair in item.LocalValues)
{
DbPropertyEntry propertyEntry = dbEntry.Property(propertyPair.Key);
object value = propertyPair.Value;
if (value == null)
{
// If the property value is null, we set null in the item too.
propertyEntry.CurrentValue = null;
continue;
}
Type type = null;
if (propertyEntry.CurrentValue != null)
{
type = propertyEntry.CurrentValue.GetType();
}
else
{
// If property does not have value now, will get property type from model
var propertyInfo = dbEntry.Entity.GetType().GetProperty(propertyPair.Key);
type = propertyInfo.PropertyType;
}
if (propertyEntry is DbComplexPropertyEntry)
{
var dic = value as IReadOnlyDictionary<string, object>;
if (dic == null)
{
throw new NotSupportedException(string.Format(
CultureInfo.InvariantCulture,
Resources.UnsupportedPropertyType,
propertyPair.Key));
}
value = Activator.CreateInstance(type);
SetValues(value, type, dic);
}
propertyEntry.CurrentValue = ConvertToEfValue(type, value);
}
}
}
示例10: 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();
}
示例11: SaveCellAsync
private async Task SaveCellAsync(DbEntityEntry<Cell> cellEntry)
{
cellEntry.Property(row => row.Value).IsModified = true;
await _unitOfWork.SaveChangesAsync();
}
示例12: ApplyAuditLog
public void ApplyAuditLog(DbContext workingContext, DbEntityEntry entry, LogOperation logOperation)
{
var includedProperties = new List<string>();
var entityKey = workingContext.GetEntityKey(entry.Entity);
var entityType = entry.Entity.GetType();
if (entry.IsAttr<AuditableAttribute>())
{
var props = entityType.GetProperties().Where(pi => !pi.IsAttr<NotAuditableAttrubute>());
includedProperties.AddRange(props.Select(pi => pi.Name));
}
else
{
var props = entityType.GetProperties()
.Where(p => p.IsAttr<AuditableAttribute>() && !p.IsAttr<NotAuditableAttrubute>());
includedProperties.AddRange(props.Select(pi => pi.Name));
}
if (entry.State == EntityState.Modified)
{
var originalValues = workingContext.Entry(entry.Entity).GetDatabaseValues();
var changedProperties = (from propertyName in originalValues.PropertyNames
let propertyEntry = entry.Property(propertyName)
let currentValue = propertyEntry.CurrentValue
let originalValue = originalValues[propertyName]
where (!Equals(currentValue, originalValue) && includedProperties.Contains(propertyName))
select new ChangedProperty
{
Name = propertyName,
CurrentValue = currentValue,
OriginalValue = originalValue
}).ToArray();
if (changedProperties.Any())
{
foreach (var log in changedProperties.Select(changedProperty => new AuditLog
{
Created = DateTime.Now,
EntityFullName = entry.Entity.GetType().FullName,
Entity = Utils.Serialize(entry.Entity),
EntityIdBytes = Utils.Serialize(entry.Entity),
Operation = logOperation,
OldValue = changedProperty.OriginalValue.ToString(),
NewValue = changedProperty.CurrentValue.ToString(),
PropertyName = changedProperty.Name
}))
{
AuditLogs.Add(log);
}
SaveChanges();
}
}
else
{
var log = new AuditLog
{
Created = DateTime.Now,
EntityFullName = entry.Entity.GetType().FullName,
Entity = Utils.Serialize(entry.Entity),
EntityIdBytes = Utils.Serialize(entityKey),
Operation = logOperation,
};
AuditLogs.Add(log);
SaveChanges();
}
}