I have a Save
method to add/update a contact. I designed an asynchronous method. But I'm not able to get the records from the database.
Kindly have a look at the code:
public async Task<bool> SaveContact(Contact contact)
{
bool flag = false;
try
{
if(contact != null)
{
using(var dbContext = DBContext())
{
ContactEDB contactObj = new ContactEDB();
if(contact.Id > 0)
{
contactObj = await dbContext.Contact.FirstOrDefaultAsync(a => a.Id == contact.Id);
// The local variable "contactObj" always return NULL while on debugging
if(contactObj != null)
{
contactObj.FirstName = "John";
contactObj.LastName = "Smith";
}
}
else
{
contactObj = new contactObj()
{
FirstName = "John";
LastName = "Smith";
};
dbContext.Contact.Add(contactObj);
}
dbContext.SaveChanges();
}
}
}
catch(Exception ex)
{
// log error
}
return flag;
}
In the above code, it always returns NULL
if(contactObj != null)
{
contactObj.FirstName = "John";
contactObj.LastName = "Smith";
};
Kindly assist me anyone to fulfill the requirements.
Note: the said method is in a library. Kindly assist me how to utilize this method using asynchronously.