erasmo carlos
erasmo carlos

Reputation: 682

If ID exists, delete before insert to avoid duplication (C# / ASP.NET)

I am using the code below to insert records into a SQL Server table. The code works perfectly, but the part I need to add is a check to make sure that the ID passed does not exist before inserting it. If records with the passed ID exists, those records need to be deleted, and then inserted again.

Would it be possible to ask for help to figure out the part that would check the ID before the insert?

I will continue to try to figure it out on my own, but I hope someone could offer some help if possible.

Here is my code:

public ActionResult InsertData(List<Meeting> meeting)
{
    bool status = false;

    if (ModelState.IsValid)
    {
        using (MeetingEntities db = new MeetingEntities())
        {
            foreach (var i in meeting)
            {
                db.Meeting.Add(i);
            }

            db.SaveChanges();
            status = true;
        }
    }

    return new JsonResult { Data = new { status = status } };
}

Table Definition

Thank you,

Erasmo

Upvotes: 0

Views: 378

Answers (2)

Rahul
Rahul

Reputation: 77876

Check it against our meeting list before adding to the context object like

            using (MeetingEntities db = new MeetingEntities())
            {
                foreach (var i in meeting)
                {
                    if(!meeting.Any(x => x.ID == i.ID)) {
                    db.Meeting.Add(i);
                   }
                }
                db.SaveChanges();
                status = true;
            }

You said * I need to check and if exists delete those records with that MeetingId* .. then you can do something like below

var meetingIds = meeting.Select(x => x.ID).ToList();
db.Meeting.Where(x => meetingIds.Contains(x.ID))
               .ToList().ForEach(db.Meeting.DeleteObject);
db.SaveChanges();

Well you can combine of this operations

            using (MeetingEntities db = new MeetingEntities())
            {
                //insert operation
                foreach (var i in meeting)
                {
                    if(!meeting.Any(x => x.ID == i.ID)) {
                    db.Meeting.Add(i);
                   }
                }

                //Delete Operation
              var meetingIds = meeting.Select(x => x.ID).ToList();
              db.Meeting.Where(x => meetingIds.Contains(x.ID))
               .ToList().ForEach(db.Meeting.DeleteObject);

                // Save the changes
                db.SaveChanges();
                status = true;
            }

Upvotes: 1

federico scamuzzi
federico scamuzzi

Reputation: 3778

maybe try to check if is already present if not insert it.. like:

public ActionResult InsertData(List<Meeting> meeting)
        {
            bool status = false;
            if (ModelState.IsValid)
            {
                using (MeetingEntities db = new MeetingEntities())
                {

                    foreach (var i in meeting)
                    {
                        if(db.Meeting.FirstOrDefault(xx=>xx. ID == i. ID) == null)
                        {
                         db.Meeting.Add(i);
                        }

                    }
                    db.SaveChanges();
                    status = true;
                }
            }
            return new JsonResult { Data = new { status = status } };
        }

Upvotes: 1

Related Questions