如何在Web Api中为JSON Post正确配置方法?

时间:2019-09-20 08:26:18

标签: c# json asp.net-core asp.net-core-webapi

我需要将数据更新/保存到DB,但是面临一个问题: 在Postman中进行测试时,我检查了以下JSON:

{
"contactId": "1",
"contactName": "t4jnesfdv",
"company": {
  "companyName":"a546fghf"
   }
}

它更新/保存表Contact中的选定记录,但还将新记录添加到其他表Company中。

在Postman中进行测试时,我检查了以下JSON:

{
"contactId": "1",
"contactName": "t4jnesfdv",
"company": {
           "companyId":"2",
           "companyName":"a546fghf"
      }
}

获取500个内部服务器错误。

接口

public interface IContactService
{
    Task<bool> SaveContact(ContactModel contact);
}

服务

public async Task<bool> SaveContact(ContactModel contactModel)
{
    using (ContactDBContext db = new ContactDBContext())
    {
        Contacts contact = db.Contacts.Where
                 (x => x.ContactId == contactModel.ContactId).FirstOrDefault();
        if (contact == null)
        {

            contact = new Contacts()
            {
                ContactName = contactModel.ContactName,
                Company = contactModel.Company
            };
            db.Contacts.Add(contact);

        }
        else
        {
            contact.ContactName = contactModel.ContactName;
            contact.Company = contactModel.Company;
        }

        return await db.SaveChangesAsync() >= 1;
    }
}

控制器

[Route("api/[controller]")]
public class ContactController : Controller
{
    [HttpPost]
    [Route("SaveContact")]
    public async Task<IActionResult> SaveContact([FromBody] ContactModel model)
    {
        return Ok(await _contactService.SaveContact(model));
    }
}

模型

public class CompanyModel
    {
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }
        public ICollection<Contacts> Contacts { get; set; }
    }
public class ContactModel
    {
        public int ContactId { get; set; }
        public string ContactName { get; set; }
        public Company Company { get; set; }
    }

实体

public class Company
    {
        public int CompanyId { get; set; }
        [StringLength(150)]
        public string CompanyName { get; set; }
        public ICollection<Contacts> Contacts { get; set; }
    }
public class Contacts
    {
        [Key]
        public int ContactId { get; set; }
        public string ContactName { get; set; }
        public Company Company { get; set; }
    }

我希望保存/更新表Contact中的记录与表Company中的现有公司,而不向表Company中添加新记录

3 个答案:

答案 0 :(得分:0)

我认为 Company 是数据库中的子表,因此在进行更新的情况下,您应该首先从数据库中获取该公司对象,然后使用其对象更新其值

Contacts contact = db.Contacts.Where(x => x.ContactId == contactModel.ContactId).FirstOrDefault();

Company company = db.Companies.Where(x => x.ContactId == contactModel.ContactId).FirstOrDefault();

 contact.ContactName = contactModel.ContactName;
 contact.Company.CompanyName = contactModel.Company.CompanyName;

答案 1 :(得分:0)

尝试这个

public async Task<bool> SaveContact(ContactModel contactModel)
    {
        using (ContactDBContext db = new ContactDBContext())
        {
            Contacts contact = db.Contacts.Where
                     (x => x.ContactId == contactModel.ContactId).FirstOrDefault();
            if (contact == null)
            {

                contact = new Contacts()
                {
                    ContactName = contactModel.ContactName,
                    Company = contactModel.Company
                };
                db.Contacts.Add(contact);

            }
            else
            {
                contact.ContactName = contactModel.ContactName;
                contact.Company.CompanyId = contactModel.Company.CompanyId;
            }

            return await db.SaveChangesAsync() >= 1;
        }
    }

答案 2 :(得分:0)

我重写了我的联系人实体和模型(并更新了数据库):

public class Contacts
    {
        [Key]
        public int ContactId { get; set; }
        public string ContactName { get; set; }
        [Display(Name = "Company")]
        public int? CompanyId { get; set; }
        [ForeignKey("CompanyId")]
        public Company Companies { get; set; }
    }

重写服务:

public async Task<bool> SaveContact(ContactModel contactModel)
        {
            using (ContactDBContext db = new ContactDBContext())
            {
                Contacts contact = db.Contacts.Where
                         (x => x.ContactId == contactModel.ContactId).FirstOrDefault();
                if (contact == null)
                {
                        contact = new Contacts()
                    {
                        ContactName = contactModel.ContactName,
                        CompanyId = contactModel.CompanyId
                };
                    db.Contacts.Add(contact);
                    }
                else
                {
                    contact.ContactName = contactModel.ContactName;
                    contact.CompanyId = contactModel.CompanyId;
                }

                return await db.SaveChangesAsync() >= 1;
            }
        }

并使用以下JSON在Postman中测试了我的Web Api:

{
        "contactId": 2,
        "contactName": "1try93445",
        "companyId": "7"
    }

一切正常。