Sushant Torankar

Sushant Torankar

  • 1.4k
  • 220
  • 22.4k

About asp.net web api core PUT method

Jun 13 2022 8:08 AM

hello,

I am new to asp.net web api core 5.0 entity framework database first approach

I am using PUT method to update details of my CONTACT table. Table has more than 8 columns . I am updating only 5 columns using PUT verb. It is updating the details, but it is making other columns NULL which are not included in my code

For ex. I am not passing "CreatedDate" column in code, but it is making that column NULL 

I only want to update columns which I am ppassing to my code, the other columns should not get null

Following is my Update Repository code ;

 public async Task UpdateContactAsync(Guid id, ContactsMaster CM)
        {            
            var contact = await _context.ContactsMasters.FindAsync(id);
            if (contact != null)
            {
                contact.UserId = CM.UserId;
                contact.ContactName = CM.ContactName;
                contact.EmailAddress = CM.EmailAddress;
                contact.PhoneNumber = CM.PhoneNumber;
                contact.Relationship = CM.Relationship;

                await _context.SaveChangesAsync();
            }
        }
    }

 

Thank you


Answers (3)