none
How to get objectcontex from a control datasource RRS feed

  • Question

  • hi, I'm new in C# and Entity Framework. Is there a way to get back the ObjectContex from control datasource ?

    I have three project , two for entity framework and the last one for window form application. The winform have two datagridview which each one from each EF project. 

    The issue are when I want to savechanges for datagridview A, I have to call MyEF_A.SaveChanges() and when I want to save datagridview B,  I have to call MyEF_B.SaveChanges().

    Is there a solution, so I can call SaveChanges() method base on Form.ActiveControl (datagridview A or datagridview B) tracing back from the control.datasource to ObjectContex ?

    Thx

    Herry

    • Moved by Alexander Sun Thursday, June 7, 2012 6:42 AM Move to more appropriate forum (From:Visual C# General)
    • Changed type ngherry Thursday, June 7, 2012 11:33 AM more suitable thread
    Wednesday, June 6, 2012 10:33 AM

Answers

  • Hi ngherry,

    Welcome to MSDN Forum.

    The data binding controls know nothing about Entity Framework, so we couldn't access ObjectContext from them. Commonly, we instantiate the ObjectContext in a using statement, this is to ensure it can be disposed every time after the operation completing. So, based on this issue, we'd better instantiate an ObjectContext instance everytime we want to do operation to the database, and then call SaveChanges to persist.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ngherry Friday, June 8, 2012 1:09 PM
    Friday, June 8, 2012 8:53 AM
    Moderator

All replies

  • How did you define the datasource? If you have done something like

    A.DataSource = MyEF_A

    then I think you should be able to cast back the DataSource:

    ((ObjectContext)A.DataSource).SaveChanges()

    Wednesday, June 6, 2012 11:34 AM
  • that is exactly what i wish for, the datagridview datasource from bindingsource dan bindingsource from List<TEntity>, I do successfull casting datagridview.datasource back to bindingsource but noluck casting back bindingsource back to ((ObjectContext)A.DataSource).SaveChanges(), compiler error.

    /*ERP_AR and ERP_AP is my Entity Framework Container*/

    ERP_AR oObjectContex_AR = New ERP_AR();
    ERP_AP oObjectContex_AP = New ERP_AP();
    List<MyTable> oEntitys_AR = oObjectContex_AR.MyTables.ToList();
    List<MyTable> oEntitys_AP = oObjectContex_AP.MyTables.ToList();


    BindingSource bs_AR = new BindingSource();
    BindingSource bs_AP = new BindingSource();
    bs_AR.DataSource = oEntitys_AR;
    bs_AP.DataSource = oEntitys_AP;


    DataGridView1.DataSource = bs_AR;
    DataGridView2.DataSource = bs_AP;



    /*Successfull in casting back to BindingSource*/
    BindingSource bs_temp;
    bs_temp = (BindingSource) DataGridView1.DataSource;


    /*Not success in casting back to oObjectContex*/
    ObjectContex oOC_Temp;
    oOC_Temp = (ObjectContex) bs_temp; --> Cannot cast


    /*Could we do this ?
    oOC_Temp.SaveChanges(); */


    /*Instead of*/
    oObjectContex_AR.SaveChanges();
    oObjectContex_AP.SaveChanges();


    any suggestion ?

    Thursday, June 7, 2012 4:32 AM
  • Did you know that there is a special forum for the Entity framework and  Linq to Entities

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads


    Success
    Cor

    Thursday, June 7, 2012 6:21 AM
  • thx for the info, i will check there

    Thursday, June 7, 2012 11:30 AM
  • Hi ngherry,

    Welcome to MSDN Forum.

    The data binding controls know nothing about Entity Framework, so we couldn't access ObjectContext from them. Commonly, we instantiate the ObjectContext in a using statement, this is to ensure it can be disposed every time after the operation completing. So, based on this issue, we'd better instantiate an ObjectContext instance everytime we want to do operation to the database, and then call SaveChanges to persist.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by ngherry Friday, June 8, 2012 1:09 PM
    Friday, June 8, 2012 8:53 AM
    Moderator
  • Allen, thx for the info. 
    Friday, June 8, 2012 1:17 PM