1

I have a POST Method which is working fine it takes a list of object and add it to the database with Entity Framework AddRange

I tried to apply the same logic with the DELETE method (takes list of objects and delete it from the database with RemoveRange) it does return 200 OK but does not delete from the database

POST

[HttpPost]
        public HttpResponseMessage Post(List<Provider_status> rows)
        {
            try
            {
                using (DbModel dbModel = new DbModel())
                {
                    dbModel.Provider_status.AddRange(rows);
                    dbModel.SaveChanges();
                }
            }
            catch { }
            return new HttpResponseMessage()
            {
                Content = new StringContent("Records Added")
            };
        }

DELETE method

[HttpDelete]
        public HttpResponseMessage Delete(List<Provider_status> rows)
        {
            try
            {
                using (DbModel dbModel = new DbModel())
                {
                    dbModel.Provider_status.RemoveRange(rows);
                    dbModel.SaveChanges();
                }
            }
            catch { }
            return new HttpResponseMessage()
            {
                Content = new StringContent("Records Deleted")
            };
        }
5
  • "not working" is not a detailed enough problem statement to be able to answer. Commented May 4, 2021 at 12:26
  • updated to: it does return 200 OK but does not delete from the database Commented May 4, 2021 at 12:29
  • Are you missing the [HttpDelete] attribute from the action? Commented May 4, 2021 at 12:31
  • i added HttpDelete but same results Commented May 4, 2021 at 12:43
  • You are catching and ignoring any error (exception), could you be missing important things the system is telling you? Commented Aug 7, 2021 at 15:28

2 Answers 2

1

Try this

foreach ( var row in rows)
{
var exRow= dbModel.Provider_status.FirstOrDefault(i=> i.Id==row.Id);
if( exRow!=null) dbModel.Provider_status.Remove(exRow);
}
dbModel.SaveChanges();

and IMHO remove [HttpDelete] from the action

Sign up to request clarification or add additional context in comments.

2 Comments

i got 2 error 1) Cannot implicitly convert type 'long' to 'bool' 2) Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
It was a typo. You have to use == instead =
0

A possible cause is that

dbModel.Provider_status.RemoveRange(rows);

Is comparing rows to Provider_status using reference equality and considering that since none match, none should be removed.

1 Comment

any idea how to fix that to be able to delete from the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.