简体   繁体   English

如何仅更新实体框架(核心)中的特定字段

[英]How to only update specific fields in Entity Framework (Core)

I'm writing an application which is pulling data from an external API and storing this into my SQL Server database using Entity Framework.我正在编写一个应用程序,它从外部 API 提取数据,并使用实体框架将其存储到我的 SQL 服务器数据库中。 I pull data from this API every day so if a row already exists the record will be updated, otherwise a new record will be created.我每天都从这个 API 中提取数据,所以如果一行已经存在,则将更新记录,否则将创建一个新记录。 This is done by checking for the Id.这是通过检查 Id 来完成的。

Now the problem I'm having is that I only want EF to update the BranchId and CustomerNameTopdesk .现在我遇到的问题是我只希望 EF 更新BranchIdCustomerNameTopdesk The CustomerNamePRTG value is added into the database by myself by hand. CustomerNamePRTG值是我自己手动添加到数据库中的。 So right now when the record is updated it's set back to NULL and my manually inserted values will be lost.所以现在当记录更新时,它被设置回 NULL 并且我手动插入的值将丢失。

Before Updating更新前

在此处输入图像描述

After Updating更新后

后

The data from the API is deserialized from JSON and stored in the data variable as a List<Customers>. API 中的数据从 JSON 反序列化,并作为List<Customers>.存储在data变量中。 I'm using the following code to check if a record already exists by Id and updating it, otherwise creating a new one.我正在使用以下代码检查 Id 是否已经存在记录并更新它,否则创建一个新记录。 As you can see it's currently updating the whole Customer record including CustomerNamePRTG which is NULL如您所见,它当前正在更新整个Customer记录,包括CustomerNamePRTG ,即 NULL

string apiResponse = await response.Content.ReadAsStringAsync();

var data = JsonConvert.DeserializeObject<List<Customers>>(apiResponse);

foreach (Customers customer in data)
{
    if (db.Customers.Any(x => x.BranchId == customer.BranchId))
    {
        db.Customers.Update(customer);
    }
    else
    {
        db.Customers.Add(customer);
    }

    db.SaveChanges();
}

Model Model

public class Customers
{
    [Key]
    [JsonProperty("id")]
    public string BranchId { get; set; }

    [JsonProperty("name")]
    public string CustomerNameTopdesk { get; set; }

    public string CustomerNamePRTG { get; set; }
}

DbContext数据库上下文

public DbSet<Customers> Customers { get; set; }

You have to update entity returned by the EF.您必须更新 EF 返回的实体。

string apiResponse = await response.Content.ReadAsStringAsync();

var data = JsonConvert.DeserializeObject<List<Customers>>(apiResponse);

foreach (Customers customer in data)
{
    var current = db.Customers.Find(customer.BranchId);
    if (current != null)
    {
        db.Entry(current).CurrentValues.SetValues(customer);
    }
    else
    {
        db.Customers.Add(customer);
    }

    db.SaveChanges();
}

You should be able to assign value to single field only.您应该只能将值分配给单个字段。

string apiResponse = await response.Content.ReadAsStringAsync();

var data = JsonConvert.DeserializeObject<List<Customers>>(apiResponse);

foreach (Customers customer in data)
{
    var record = db.Customers.Find(customer.BranchId);
    if (record != null)
    {
        record.BranchId = customer.BranchId;
        // as many fields as you need
 
        db.Customers.Update(record );
    }
    else
    {
        db.Customers.Add(customer);
    }

    db.SaveChanges();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM