I
I
Ilya2013-12-09 08:44:06
C++ / C#
Ilya, 2013-12-09 08:44:06

Updating a Record in Entity Framework

I use MSSQL Server 2008 and 2010 studio.
Trying to update the selected entry:

try
                {
                    //указываю выбраную запись в DataGrid
                    Model.paket selPaket = (paket)paketDataGrid.SelectedItem;
                    using (MagnesiumEntities _cnt = new MagnesiumEntities())
                    {
                        //выбираем запись по id
                        var pakToDetach = _cnt.pakets.FirstOrDefault(p => p.id == selPaket.id);
                        if (pakToDetach == null)
                            throw new Exception("Product already deleted");
                        //изменяю массу брутто и нетто
                        pakToDetach.mBrutto = selPaket.mBrutto;
                        pakToDetach.mNetto = selPaket.mNetto;                      
                        _cnt.SaveChanges();

                    }
                }
                
               catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

There are no errors, but nothing changes for some reason. What am I doing wrong ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LChaos, 2013-12-09
@Zerpico

Try this
_cnt.Entry(pakToDetach).State = EntityState.Modified;
_cnt.SaveChanges();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question