[英]How to prevent to change other column values into table while updating single column using Entity Framework?
Here i have a method in ASP.NET MVC.这里我在 ASP.NET MVC 中有一个方法。 What i am doing is to update single column checking every column of table is not null.
我正在做的是更新单列检查表的每一列不为空。 If null then IsModified property changing to false.
如果为 null,则 IsModified 属性更改为 false。 I have to write statement for every column.
我必须为每一列写声明。
My Sample Method -我的示例方法 -
public int ServicesEdit(helplineservice _helplineservice)
{
int result = 0;
try
{
db.Entry(_helplineservice).State = EntityState.Modified;
if (string.IsNullOrEmpty(_helplineservice.description))
{
db.Entry(_helplineservice).Property(p => p.description).IsModified = false;
}
else
{
db.Entry(_helplineservice).Property(p => p.description).IsModified = true;
}
if (string.IsNullOrEmpty(_helplineservice.title))
{
db.Entry(_helplineservice).Property(p => p.title).IsModified = false;
}
else
{
db.Entry(_helplineservice).Property(p => p.title).IsModified = true;
}
if (string.IsNullOrEmpty(_helplineservice.contactnumber))
{
db.Entry(_helplineservice).Property(p => p.contactnumber).IsModified = false;
}
else
{
db.Entry(_helplineservice).Property(p => p.contactnumber).IsModified = true;
}
//if (string.IsNullOrEmpty(_helplineservice.active.ToString()))
//{
// db.Entry(_helplineservice).Property(p => p.active).IsModified = false;
//}
//else
//{
// db.Entry(_helplineservice).Property(p => p.active).IsModified = true;
//}
db.SaveChanges();
result = 1;
}
catch (Exception ex)
{
result = 0;
}
return result;
}
Calling Above Method -调用上述方法 -
helplineservice _helplineservice = new helplineservice();
_helplineservice.helplineid =sectionid;
_helplineservice.allowedtoapp = allow;
result = _ftwCommonMethods.ServicesEdit(_helplineservice);
This code not look logical i think.我认为这段代码看起来不合逻辑。 Tell me better way to do this.
告诉我更好的方法来做到这一点。 How Can i Update Single Column of Table by not writing this much code?
如何通过不编写这么多代码来更新表的单列? Thanks in Advance.
提前致谢。
You can avoid all of the checking by loading the entity you want to update first.您可以通过首先加载要更新的实体来避免所有检查。
var context = new DbContext();
// Load entity via whatever Id parameter you have.
var entityToUpdate = context.Set<Type>().FirstOrDefault(x => x.Id == idToUpdate);
if(entityToUpdate != null)
{
entityToUpdate.Value1 = newValue1;
entityToUpdate.Value2 = newValue2;
context.SaveChanges();
}
Only Value1 and Value2 will be updated.只有 Value1 和 Value2 将被更新。 All other existing values will remain unchanged.
所有其他现有值将保持不变。
In your case, what you are doing is creating a new empty entity, then setting its Key to something that already exists in the database.在您的情况下,您正在做的是创建一个新的空实体,然后将其 Key 设置为数据库中已存在的内容。 When you attach that entity to the context, EF has no choice but to assume that all the values in that entity are the new updated values.
当您将该实体附加到上下文时,EF 别无选择,只能假设该实体中的所有值都是新的更新值。 This is standard behavior for EF.
这是 EF 的标准行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.