none
LINQ to Entities RRS feed

  • Question

  • I have a database and use LINQ to Entities for data queries. Where is the best place to instantiate the model? I have:

    Public frmMain
    {
        MaterialEntities myEnt = new MaterialEntities();
     
        var selectedItems = from i in myEnt.Items // this works fine
                            from c in myEnt.Categories
                            ........................
                            ........................
    
    }
    private void cmbCategories_SelecetedIndexChanged(object Sender, EventArgs e)
    {   
        MaterialEntities myEnt = new MaterialEntities(); // need this due to scope. Is it  good practice?
        var selectedItems = from i in myEnt.Items
                            from c in myEnt.Categories
                            ........................
                            ........................
    }
    
    
    

     

    If I want to fill combo boxes or lists or datagrids I have to reinstantiate the model due to scope as in the above code. Is this the correct way of doing it?

    Many thanks.

    Clive


    Still trying to program
    • Moved by CoolDadTx Wednesday, September 28, 2011 3:00 AM EF related (From:Visual C# General)
    Tuesday, September 27, 2011 3:35 PM

Answers

  • Hi,

    You should instantiate the model wherever you need it and you should make sure that you dispose it by using the using statement :

    using(MaterialEntities myEnt = new MaterialEntities())
    {
      //  ... 
    }
    
    Kind regards,


    aelassas.free.fr
    • Marked as answer by Jonsey Wednesday, September 28, 2011 6:31 AM
    Tuesday, September 27, 2011 4:03 PM
  • On 9/27/2011 11:35 AM, Jonsey wrote:
    > I have a database and use LINQ to Entities for data queries. Where is
    > the best place to instantiate the model? I have:
    >
    > Public frmMain
    > {
    >      MaterialEntities myEnt =new  MaterialEntities();
    >
    >      var  selectedItems =from  iin  myEnt.Items// this works fine
    >                          from  cin  myEnt.Categories
    >                          ........................
    >                          ........................
    >
    > }
    > private  void  cmbCategories_SelecetedIndexChanged(object  Sender, EventArgs e)
    > {
    >      MaterialEntities myEnt =new  MaterialEntities();// need this due to scope. Is it  good practice?
    >      var  selectedItems =from  iin  myEnt.Items
    >                          from  cin  myEnt.Categories
    >                          ........................
    >                          ........................
    > }
    >
    >
    >
    > If I want to fill combo boxes or lists or datagrids I have to
    > reinstantiate the model due to scope as in the above code. Is this the
    > correct way of doing it?
    >
         try
        {
            using(var myEnt =new  MaterialEntities())
            {
               var  selectedItems =from  iin  myEnt.Items// this works fine
                              from  cin  myEnt.Categories
                              ........................
                              ........................
           }
        }
        catch(Exception ex)
        {
           Messgaebox.Show(ex.Tostring());
         }
     
    The above is what you should be doing or some form of it every time you
    want to do something with any database connection and working with
    database data for CRUD (Create, Read, Update and Delete) operations.
     
     
    • Marked as answer by Jonsey Wednesday, September 28, 2011 6:31 AM
    Tuesday, September 27, 2011 6:01 PM

All replies

  • Hi,

    You should instantiate the model wherever you need it and you should make sure that you dispose it by using the using statement :

    using(MaterialEntities myEnt = new MaterialEntities())
    {
      //  ... 
    }
    
    Kind regards,


    aelassas.free.fr
    • Marked as answer by Jonsey Wednesday, September 28, 2011 6:31 AM
    Tuesday, September 27, 2011 4:03 PM
  • On 9/27/2011 11:35 AM, Jonsey wrote:
    > I have a database and use LINQ to Entities for data queries. Where is
    > the best place to instantiate the model? I have:
    >
    > Public frmMain
    > {
    >      MaterialEntities myEnt =new  MaterialEntities();
    >
    >      var  selectedItems =from  iin  myEnt.Items// this works fine
    >                          from  cin  myEnt.Categories
    >                          ........................
    >                          ........................
    >
    > }
    > private  void  cmbCategories_SelecetedIndexChanged(object  Sender, EventArgs e)
    > {
    >      MaterialEntities myEnt =new  MaterialEntities();// need this due to scope. Is it  good practice?
    >      var  selectedItems =from  iin  myEnt.Items
    >                          from  cin  myEnt.Categories
    >                          ........................
    >                          ........................
    > }
    >
    >
    >
    > If I want to fill combo boxes or lists or datagrids I have to
    > reinstantiate the model due to scope as in the above code. Is this the
    > correct way of doing it?
    >
         try
        {
            using(var myEnt =new  MaterialEntities())
            {
               var  selectedItems =from  iin  myEnt.Items// this works fine
                              from  cin  myEnt.Categories
                              ........................
                              ........................
           }
        }
        catch(Exception ex)
        {
           Messgaebox.Show(ex.Tostring());
         }
     
    The above is what you should be doing or some form of it every time you
    want to do something with any database connection and working with
    database data for CRUD (Create, Read, Update and Delete) operations.
     
     
    • Marked as answer by Jonsey Wednesday, September 28, 2011 6:31 AM
    Tuesday, September 27, 2011 6:01 PM
  • Hi

    Why you had to instantiate new instance of MaterialEntities every time there is  Index changed? This itself impact the performance right?

    I can guess MaterialEntities will have necessary mappings, Entity contextx collections DbSets or ObjectSets...etc Loading will initialize again..


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Wednesday, September 28, 2011 12:05 AM
  • Thank you for your help. I didn't know about "using" in code like that.
    Still trying to program
    Wednesday, September 28, 2011 6:32 AM