我需要为给定实体对象更新除property1和property2以外的所有字段。具有此代码:
[HttpPost]
public ActionResult Add(object obj)
{
if (ModelState.IsValid)
{
context.Entry(obj).State = System.Data.EntityState.Modified;
context.SaveChanges();
}
return View(obj);
}
如何进行更改以将obj.property1和obj.property2添加一个异常,以使其不使用此代码进行更新?
假设您具有要排除的属性的集合:
var excluded = new[] { "property1", "property2" };
使用.NET 4.5上的EF5,您可以执行此操作:
var entry = context.Entry(obj);
entry.State = EntityState.Modified;
foreach (var name in excluded)
{
entry.Property(name).IsModified = false;
}
[这使用了.NET 4.5上EF5的一项新功能,即使在先前将属性设置为已修改的情况下,该属性也可以将其设置为未修改。
[在.NET 4上使用EF 4.3.1或EF5时,您可以改为执行此操作:
var entry = context.Entry(obj);
foreach (var name in entry.CurrentValues.PropertyNames.Except(excluded))
{
entry.Property(name).IsModified = true;
}
您无法定义此类例外。但是,您可以将单个属性标记为已修改:
context.Entry(obj).Property(o => o.Property3).IsModified = true;
context.Entry(obj).Property(o => o.Property4).IsModified = true;
// etc.
注意,一旦将整个实体的状态标记为IsModified
,就不支持将false
设置为Modified
。
出于您的目的,我实际上更希望从数据库中加载实体,然后使用常规的更改跟踪对其进行更新:
var objInDB = context.Objects.Single(o => o.Id == obj.Id);
obj.Property1 = objInDB.Property1;
obj.Property2 = objInDB.Property2;
context.Entry(objInDB).CurrentValues.SetValues(obj);
context.SaveChanges();
这个问题已经很好回答,但是我想为任何想使用它的人提供扩展方法。
此代码是为EF 4.3.1开发的
//You will need to import/use these namespaces
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
//Update an entity object's specified columns, comma separated
//This method assumes you already have a context open/initialized
public static void Update<T>(this DbContext context, T entityObject, params string[] properties) where T : class
{
context.Set<T>().Attach(entityObject);
var entry = context.Entry(entityObject);
foreach(string name in properties)
entry.Property(name).IsModified = true;
context.SaveChanges();
}
用法示例
using (FooEntities context = new FooEntities())
{
FooEntity ef = new FooEntity();
//For argument's sake say this entity has 4 columns:
// FooID (PK), BarID (FK), Name, Age, CreatedBy, CreatedOn
//Mock changes
ef.FooID = 1;
ef.Name = "Billy";
ef.Age = 85;
context.Update<FooEntity>(ef, "Name", "Age"); //I only want to update Name and Age
}
这是一个适用于.net CORE的更新,也许可以帮助需要通用语言并希望根据不同条件排除某些属性的人。
[我正在使用反射来遍历属性并基于其属性值进行更新,在这种情况下,例如,我排除了null属性。
public virtual TEntity Update(TEntity entity)
{
dbSet.Attach(entity);
dbContext.Entry(entity).State = EntityState.Modified;
var entry = dbContext.Entry(entity);
Type type = typeof(TEntity);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.GetValue(entity, null) == null)
{
entry.Property(property.Name).IsModified = false;
}
}
dbContext.SaveChanges();
return entity;
}
上面的答案(大多数答案)使用DbContext。对于那些正在使用ObjectContext的用户,可以使用这些解决方案。
这里严格是ObjectContext的解决方案(EF5 .NET 4.5:
ctx.AddObject("ENTITYNAME", item);
ctx.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
var entry = ctx.ObjectStateManager.GetObjectStateEntry(item);
entry.RejectPropertyChanges("PROPERTY_TO_EXCLUDE");