本文整理汇总了C#中DbContext.Entry方法的典型用法代码示例。如果您正苦于以下问题:C# DbContext.Entry方法的具体用法?C# DbContext.Entry怎么用?C# DbContext.Entry使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DbContext
的用法示例。
在下文中一共展示了DbContext.Entry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateContextItem
public static object UpdateContextItem(DbContext context,Object obj)
{
//将foreign key制空
obj.GetType().GetProperties().ToList().ForEach(delegate (PropertyInfo pi)
{
var att = pi.GetCustomAttribute(typeof(ForeignKeyAttribute));
if (att != null)
{
pi.SetValue(obj, null);
}
});
//修改操作为修改
context.Entry(obj).State = EntityState.Modified;
//重新加载foreign key的对象。
obj.GetType().GetProperties().ToList().ForEach(delegate (PropertyInfo pi)
{
var att = pi.GetCustomAttribute(typeof(ForeignKeyAttribute));
if (att != null)
{
context.Entry(obj).Reference(pi.Name).Load();
}
});
return obj;
}
示例2: Update
public void Update(DbContext DbContext,ManufacturePlanItem entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例3: Update
public void Update(DbContext DbContext,ProductWareStock entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例4: Update
public void Update(DbContext DbContext,ArticleAnnex entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例5: Update
public void Update(DbContext DbContext,CompanyClientType entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例6: Update
public void Update(DbContext DbContext,Facility entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例7: Update
public void Update(DbContext DbContext,MaterialPurchasePlan entity)
{
EntityState state = DbContext.Entry(entity).State;
if (state == EntityState.Detached)
{
DbContext.Entry(entity).State = EntityState.Modified;
}
}
示例8: CheckValue
public void CheckValue(DbContext ctx, object actual)
{
var propertyType = Accessor.MemberType;
var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
var entitySet = objectContext.GetEntitySet(propertyType);
var keyMembers = entitySet.ElementType.KeyMembers;
ctx.Entry(actual).Reference(Accessor.Name).Load();
var actualEntity = Accessor.GetValue(actual);
if (actualEntity == null)
{
throw new AssertionException(ExpectedEntity.Dump(), "NULL");
}
foreach (var keyMember in keyMembers)
{
var accessor = new PropertyAccessor(propertyType.GetProperty(keyMember.Name));
var actualKeyValue = accessor.GetValue(actualEntity);
var expectedKeyValue = accessor.GetValue(ExpectedEntity);
if (!expectedKeyValue.Equals(actualKeyValue))
{
throw new AssertionException(ExpectedEntity.Dump(), actualEntity.Dump());
}
}
}
示例9: AppendDescriptionUsingContext
private static void AppendDescriptionUsingContext(DbContext context, StringBuilder builder, EntityType type, ActionType aType, object entity)
{
string additionalInfo = string.Empty;
string identity = string.Empty;
var enrty = context.Entry(entity);
var prop =
enrty.Entity.GetType()
.GetProperties()
.FirstOrDefault(c => c.GetCustomAttributes(typeof(KeyAttribute), true).FirstOrDefault() != null);
var name = enrty.Entity.GetType().GetProperties().FirstOrDefault(c => c.Name.Contains("Name"));
identity = CreateIdentityString(entity, prop, identity, name);
if (aType == ActionType.Updating)
{
additionalInfo = string.Format("Были изменены следующие поля: {0}",
string.Join(",", enrty.CurrentValues.PropertyNames));
}
if (aType != ActionType.Import || aType != ActionType.Export)
{
builder.Append(string.Format("Сущность \"{0}\" {1} была {2}.{3}", type.GetEntityTypeName(), identity,
aType.GetActionTypeName(), additionalInfo));
}
}
示例10: Insert
public void Insert(DbContext DbContext, IEnumerable<Facility> entities)
{
try
{
DbContext.Configuration.AutoDetectChangesEnabled = false;
foreach (Facility entity in entities)
{
DbContext.Entry(entity).State = EntityState.Added;
}
}
finally
{
DbContext.Configuration.AutoDetectChangesEnabled = true;
}
}
示例11: Delete
public void Delete(DbContext DbContext, IEnumerable<CompanyClientType> entities)
{
try
{
DbContext.Configuration.AutoDetectChangesEnabled = false;
foreach (CompanyClientType entity in entities)
{
DbContext.Entry(entity).State = EntityState.Deleted;
}
}
finally
{
DbContext.Configuration.AutoDetectChangesEnabled = true;
}
}
示例12: Delete
public void Delete(DbContext DbContext, IEnumerable<MaterialPurchasePlan> entities)
{
try
{
DbContext.Configuration.AutoDetectChangesEnabled = false;
foreach (MaterialPurchasePlan entity in entities)
{
DbContext.Entry(entity).State = EntityState.Deleted;
}
}
finally
{
DbContext.Configuration.AutoDetectChangesEnabled = true;
}
}
示例13: Delete
public void Delete(DbContext DbContext, IEnumerable<ProductWareStock> entities)
{
try
{
DbContext.Configuration.AutoDetectChangesEnabled = false;
foreach (ProductWareStock entity in entities)
{
DbContext.Entry(entity).State = EntityState.Deleted;
}
}
finally
{
DbContext.Configuration.AutoDetectChangesEnabled = true;
}
}
示例14: Delete
public void Delete(DbContext DbContext, IEnumerable<ArticleAnnex> entities)
{
try
{
DbContext.Configuration.AutoDetectChangesEnabled = false;
foreach (ArticleAnnex entity in entities)
{
DbContext.Entry(entity).State = EntityState.Deleted;
}
}
finally
{
DbContext.Configuration.AutoDetectChangesEnabled = true;
}
}
示例15: WritePropertyValidationResults
public static void WritePropertyValidationResults(DbContext context, object model, params string[] propertyNames)
{
var entity = context.Entry(model);
Console.WriteLine("Type: {0}", model.GetType().Name);
foreach (string propertyName in propertyNames)
{
Console.WriteLine("\nProperty: {0}", propertyName);
Console.WriteLine("Value: {0}", entity.Property(propertyName).CurrentValue);
var results = entity.Property(propertyName).GetValidationErrors();
Console.WriteLine("Passed Validation: {0}",!results.Any());
foreach (DbValidationError dbValidationError in results)
{
Console.WriteLine(" - {0}", dbValidationError.ErrorMessage);
}
}
}