【问题标题】:Web API PUT nested ICollectionWeb API PUT 嵌套的 ICollection
【发布时间】:2013-12-24 17:23:13
【问题描述】:

如何使用 ASP.Net Web API 2 放置嵌套的 ICollection?我会解释的。

我正在使用带有 Web API 2 的实体框架。我的公司如下:

public class Company
{
    public int ID { get; set; }

    [Required]
    public string Name { get; set; }

    ..

    public virtual CountryRegion CountryRegion { get; set; }
    public virtual ICollection<Organization> Organizations { get; set; }
}

我有基于 Company 模型的标准生成的 Web API 2 控制器。这是我的 PUT 函数:

// PUT api/Company/5
public IHttpActionResult PutCompany(int id, Company company)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    if (id != company.ID)
    {
        return BadRequest();
    }

    var entityToUpdate = db.Companies.Find(id);
    db.Entry<Company>(entityToUpdate).CurrentValues.SetValues(company);
    db.Entry<Company>(entityToUpdate).State = EntityState.Modified;

    db.Entry<CountryRegion>(entityToUpdate.CountryRegion).CurrentValues.SetValues(company.CountryRegion);

    try
    {
        db.SaveChanges();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!CompanyExists(id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }

    return StatusCode(HttpStatusCode.NoContent);
}

由于默认生成的 PUT 函数中的 db.Entry(company).State = EntityState.Modified; 不起作用,我已将其替换为如上所述的 on another question

当我发送如下 PUT 调用时,保存名称和 CountryRegion 但不保存组织。

$.ajax({
    type: "PUT",
    url: "/api/company/2",
    data: {
        ID:2, 
        Name: "Test Company", 
        CountryRegion: {
            ID: 2, 
            Name: "United States"
        }, 
        Organizations: [
            {
                ID: 10,
                Name: "Test Org"
            },
            {
                ID: 22,
                Name: "Test Org 2"
            }
        ]
    }
});

如何修改我的控制器代码以便组织保存?组织与公司是多对多的。我想也许我可以删除与该公司关联的所有现有组织,然后保存所有新组织,但我对如何真正让这些新组织保存并与该公司关联感到困惑。

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework asp.net-web-api


    【解决方案1】:

    只是在黑暗中拍摄,但您的 FK 在数据库中设置正确吗?我已经很多年没有使用实体框架了,但相关对象没有保存听起来可能是那个级别的问题,而不是你的 Web API。

    还有,为什么没有:

    db.Entry<CountryRegion>(entityToUpdate.Organizations ).CurrentValues.SetValues(company.Organizations);
    

    ?

    【讨论】:

    • 很确定它是正确的。当我使用默认的 POST 方法时,一切都会正确插入。我已经尝试过该代码 sn-p (使用正确的组织类型),但它给出了一个错误,因为组织是单数的,我试图将多个因此 ICollection 问题。
    • 我尝试过db.Entry&lt;ICollection&lt;Organization&gt;&gt;(entityToUpdate.Organizations).CurrentValues.SetValues(company.Organizations);,它返回The entity type HashSet1 is not part of the model for the current context.,而db.Entry&lt;Organization&gt;(entityToUpdate.Organizations).CurrentValues.SetValues(company.Organizations); 甚至无法构建。
    【解决方案2】:

    我能够使用try 块上方的以下内容解决此问题,但我不一定相信这是正确的方法:

    //remove current organizations
    foreach (var organization in db.Organizations)
    {
        entityToUpdate.Organizations.Remove(organization);
    }
    //add updated organizations
    entityToUpdate.Organizations = company.Organizations;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 2015-02-03
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多