简体   繁体   English

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

[英]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.

相关问题 如果它不为null,如何防止Entity Framework更新某个列? - How to prevent Entity Framework from updating a certain column if it's not null? 使用实体框架核心更新数据库中的列 - Updating column in database using entity framework core 如何使用实体框架从SQL表中获取两个列值的总和? - How to get sum of two column values from SQL table using Entity Framework? 更改实体框架7中的列? - Change column in Entity Framework 7? 如何使用Entity Framework获取单个列? - How to get a single column with Entity Framework? 如何使用实体框架选择单列? - How to select a single column with Entity Framework? 无法首先使用实体​​框架代码将其他指定的列设置为主键,而不是将ID列设置为表 - unable to set other specified column as primary key instead of Id column to table using entity framework code first 如何使用实体框架 6 更新具有唯一列的表上的条目 - How to update an entry on table with unique column using Entity Framework 6 如何使用 linq 对 Entity Framework 中的列求和并将其与另一个表连接 - How sum a column in Entity Framework using linq and join it with another table 如何通过实体框架中数据库的一列上的多个值与其他列上的不同值获取数据? - How to get data by mutiple values on one column with different values on other columns from database in Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM