C# EF核心更新链接对象

C# EF核心更新链接对象,c#,ef-core-3.0,C#,Ef Core 3.0,我正在尝试使用efcore更新一家公司及其联系方式,如示例JSON请求中所示 PUT https://localhost:6001/api/Company { "CompanyId": 6, "Name": "string", "ContactDetail": { "Name": "string", "EmailAddress": "string", "Address1": "string", "Address2": "string", "A

我正在尝试使用efcore更新一家公司及其联系方式,如示例JSON请求中所示

PUT https://localhost:6001/api/Company

{
  "CompanyId": 6,
  "Name": "string",
  "ContactDetail": {
    "Name": "string",
    "EmailAddress": "string",
    "Address1": "string",
    "Address2": "string",
    "Address3": "string",
    "PostCode": "string",
    "Telephone": "string"
  }
}
生成的EF Scaffold如下所示

public partial class Company
{
    public long CompanyId { get; set; }
    public string Name { get; set; }
    public virtual ContactDetail ContactDetail { get; set; }
}

public partial class ContactDetail
{
    public ContactDetail()
    {
        Company = new HashSet<Company>();
    }

    public long ContactDetailId { get; set; }
    public string Name { get; set; }
    public string EmailAddress { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string PostCode { get; set; }
    public string Telephone { get; set; }

    public virtual ICollection<Company> Company { get; set; }
}
更新-我现在使用的解决方案是获取ContactDetail id,然后将其重新分配给对象

public Company Update(Company company)
{
    var currentCompany = GetById(companyId, true);

    // Update the contact detail id so that it know what one to update
    company.ContactDetail.ContactDetailId = currentCompany.ContactDetailId;

    _bigLoaderContext.Update(company);
    _bigLoaderContext.SaveChanges();
}
public Company Update(Company company)
{
    var currentCompany = GetById(companyId, true);

    // Update the contact detail id so that it know what one to update
    company.ContactDetail.ContactDetailId = currentCompany.ContactDetailId;

    _bigLoaderContext.Update(company);
    _bigLoaderContext.SaveChanges();
}
我加载对象以获取值时出错,因此必须使用GetById方法中的AsNoTracking获取该值

_bigLoaderContext.Company.AsNoTracking().FirstOrDefault(f => f.CompanyId == companyId);
_bigLoaderContext.Company.AsNoTracking().FirstOrDefault(f => f.CompanyId == companyId);

我现在使用的解决方案是获取ContactDetail id,然后将其重新分配给对象

public Company Update(Company company)
{
    var currentCompany = GetById(companyId, true);

    // Update the contact detail id so that it know what one to update
    company.ContactDetail.ContactDetailId = currentCompany.ContactDetailId;

    _bigLoaderContext.Update(company);
    _bigLoaderContext.SaveChanges();
}
public Company Update(Company company)
{
    var currentCompany = GetById(companyId, true);

    // Update the contact detail id so that it know what one to update
    company.ContactDetail.ContactDetailId = currentCompany.ContactDetailId;

    _bigLoaderContext.Update(company);
    _bigLoaderContext.SaveChanges();
}
我在加载对象以获取值时出错,因此必须使用GetById方法中的AsNoTracking来获取该值

_bigLoaderContext.Company.AsNoTracking().FirstOrDefault(f => f.CompanyId == companyId);
_bigLoaderContext.Company.AsNoTracking().FirstOrDefault(f => f.CompanyId == companyId);
假设它正常工作