如何防止在使用实体框架更新单个列时将其他列值更改为表?

时间:2022-05-11 08:04:16

Here i have a method in 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. I have to write statement for every column.

这里我有一个ASP中的方法。净MVC。我要做的是更新单个列,检查表的每一列是否为空。如果为空,则IsModified属性变为false。我必须为每一列写语句。

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.

我认为这段代码不符合逻辑。告诉我更好的方法。我怎么能不写这么多代码就更新单列表呢?提前谢谢。

1 个解决方案

#1


1  

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. All other existing values will remain unchanged.

只有Value1和Value2将被更新。所有其他现有值将保持不变。

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. 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. This is standard behavior for EF.

在您的案例中,您所做的是创建一个新的空实体,然后将其键设置为已经存在于数据库中的某个东西。当您将该实体附加到上下文时,EF别无选择,只能假设该实体中的所有值都是新更新的值。这是EF的标准行为。

#1


1  

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. All other existing values will remain unchanged.

只有Value1和Value2将被更新。所有其他现有值将保持不变。

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. 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. This is standard behavior for EF.

在您的案例中,您所做的是创建一个新的空实体,然后将其键设置为已经存在于数据库中的某个东西。当您将该实体附加到上下文时,EF别无选择,只能假设该实体中的所有值都是新更新的值。这是EF的标准行为。