0

I have this C# Code:

using (ContractorsEntities context = new ContractorsEntities(Properties.Settings.Default.Connection))
        {
            warningBindingSource.DataSource = context.Warnings.ToList();
            educationLevelBindingSource.DataSource = context.EducationLevels.ToList();
            penaltyBindingSource.DataSource = context.Penalties.ToList();
        }

Everything is working fine without any problems but if I add new rows to GridView or updated some rows for example, how i can submit data - new rows - to Database?

I know we should call context.SaveChanges(); but it will not work.

Saleh
  • 2,487
  • 12
  • 40
  • 72
  • Have you tried calling context.SaveChanges() and it didn't work? – Nick Patsaris May 05 '13 at 21:30
  • @Circular Reference Actually no because `context` will get disposed after returning all data from database. – Saleh May 05 '13 at 21:31
  • I think you need a shared context here. – Nick Patsaris May 05 '13 at 21:36
  • @CircularReference according to this answer http://stackoverflow.com/questions/15264983/entity-framework-objectcontext-share-pros-and-cons sharing context is bad – HaBo May 06 '13 at 01:37
  • UnitOfWork is essentially a layer or abstraction so you can share your context properly. – Nick Patsaris May 06 '13 at 09:13
  • I've done a bit more digging, apparently it is a good idea to use a single context per form : http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392 – Nick Patsaris May 07 '13 at 10:41

1 Answers1

0

Use Context.Warnings.Local.ToBindingList() to do the binding

Kirsten Greed
  • 11,170
  • 26
  • 117
  • 234