Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查EntityState中单个字段的更改_C#_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

C# 检查EntityState中单个字段的更改

C# 检查EntityState中单个字段的更改,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,在我们的数据库中,我们有一个用户。用户可以有与之关联的特定元数据(如位置、年龄等)。除此之外,它们还有一个配置文件图像。 用户可以随时编辑这些内容 我们遇到的问题是,当用户去编辑他们的信息,但他们没有选择一个图像时,上一个图像会被擦除(Null) 我认为当我们去保存修改过的用户时,如果(user.profileImage==null)不保存它,我们应该能够说 我认为这将在使用以下代码的用户存储库中发挥作用: public void SaveUser(Domain.Entities.User u

在我们的数据库中,我们有一个用户。用户可以有与之关联的特定元数据(如位置、年龄等)。除此之外,它们还有一个配置文件图像。 用户可以随时编辑这些内容

我们遇到的问题是,当用户去编辑他们的信息,但他们没有选择一个图像时,上一个图像会被擦除(Null)

我认为当我们去保存修改过的用户时,如果(user.profileImage==null)不保存它,我们应该能够说

我认为这将在使用以下代码的用户存储库中发挥作用:

public void SaveUser(Domain.Entities.User user)
        {
            if (user.UserId == 0)
            {
                context.Users.Add(user);
            }
            else
            {
                context.Entry(user).State = EntityState.Modified;
                //The logic would be here
            }
            context.SaveChanges();
        }
然而,我觉得无论我们尝试什么,它都不起作用

所以我的问题是:有没有一种方法可以检查单个字段而不是整个EntityState的更改?

是的,有一种方法:

context.Users.Attach(user);
context.Entry(user).Property(u => u.Location).IsModified = true;
context.Entry(user).Property(u => u.Age).IsModified = true;
// etc.
请注意,一旦属性标记为已修改,就不能将
IsModified
设置为
false
。所以,像这样的事情

context.Entry(user).State = EntityState.Modified;
context.Entry(user).Property(u => u.Image).IsModified = false;
不起作用

编辑备选解决方案:

public void SaveUser(Domain.Entities.User user)
{
    if (user.UserId == 0)
    {
        context.Users.Add(user);
    }
    else
    {
        var userInDb = context.Users.Single(u => u.UserId == user.UserId);

        if (user.profileImage == null)
            user.profileImage = userInDb.profileImage;

        context.Entry(userInDb).CurrentValues.SetValues(user);
    }
    context.SaveChanges();
}

如果用户没有发布新图像(
user.profileImage==null
),这将保持
profileImage
的值在数据库中的状态,否则它将保存新图像。

我将图像数据存储在会话中,如果图像上载为null,我只需将会话属性插入到我的图像中,如果保留为空,则有效地保留旧图像

Session[“image”]=productService.GetProduct(id).image

然后在帖子里我说

if (image != null)
                {
                    product.ImageType = image.ContentType;
                    product.Image = new byte[image.ContentLength];
                    image.InputStream.Read(product.Image, 0, image.ContentLength);
                }
                else
                {
                    product.Image = (byte[])Session["image"];
                }

那么,你如何明确地说“如果这个字段中没有任何内容,就不要更新它?”@Johannes:除了显而易见的:
if(user.Image!=null&&user.Image.Length>0)context.Entry(user).Property(u=>u.Image).IsModified=true我认为EF没有任何选项可以自动实现这一点。毕竟,对于EF来说,“nothing in it”是一个有效的值,它将为任何其他值编写更新。老实说,这不是一个好的解决方法。你会有用户想知道为什么他们的图像消失了,仅仅因为他们在点击提交按钮之前要吃点东西。在我的回答中,我发布了一个替代方案,它基本上使用数据库作为图像已经存在的存储,而不是脆弱的会话对象。