locked
best approach to use update in database using entity framework. RRS feed

  • Question

  • Hello All,

    do anybody give me some examples on how to achieve update functionality using entity framework.

    in my scenario i am taking data without primary key.

    now i am having another column which is unique in this.

    when i am passing these values to the database it's not getting updated.

    below is my code which i am working on.

                            

    public void InsertBulkRecords(List<ShowHors> listshowhorse)
            {
                ShowHors updateShowHors = new ShowHors();
                try
                {
                    using (var context = new CurrentHorseShowEntities())
                    {
                        foreach (ShowHors item in listshowhorse)
                        {
                            var original = context.ShowHorses.Find(item.HorseId);

                            if (original.HorseId != null)
                            {
                               //Initially updateShowHors.ShowHorsesId is null. i am assigning it the 

    original.ShowHorsesId; value.

        updateShowHors.ShowHorsesId = original.ShowHorsesId;
                                updateShowHors.ShowId = item.ShowId;
                                updateShowHors.HorseCatalog = item.HorseCatalog;
                                updateShowHors.IsActive = item.IsActive;
                                //context.SaveChanges();
                                context.Entry(original).CurrentValues.SetValues(updateShowHors);
                                context.SaveChanges();
                            }

                        }

                    }
                }
                catch
                {

                    throw;
                }
            }

    where am i getting wrong kindly let me know or kindly suggest me the best approach.


    • Edited by EmpAnsar Tuesday, February 16, 2016 6:02 AM updated the lines in bold format
    Tuesday, February 16, 2016 5:59 AM

Answers

  • where am i getting wrong kindly let me know or kindly suggest me the best approach.

    use a primary key...

    Tuesday, February 16, 2016 10:23 PM