locked
is there a way to create an instance of a portion of a data model? RRS feed

  • Question

  • User-1833980242 posted

    I'm newing up this entity data model to use only one table in it.  My model isn't really complicated.  I don't know what kind of performance impacts there are by creating a new instance of everything, but it seems like it would be better to only get what is needed.

    Is there a way to create an instance of just the entity that's needed?

    var dataModel = new DatabaseEntities();

    Thursday, August 27, 2015 4:59 PM

Answers

  • User281315223 posted

    Generally you would want to use your specific entities to only pull out what you need from the database (and not instantiate the context each time). For instance if you needed to access a Widget entity, you might use something like this :

    using(var context = new DatabaseEntities())
    {
         // Get the first widget
         var widget = context.Widgets.FirstOrDefault();
    }

    Likewise, if you didn't want to actually pass across all of your data, you could always use a Select() method to only populate the properties you need into an object or another class :

    // Get the first widget
    var widget = context.Widgets.FirstOrDefault();
    
    // Return a Widget instance that only has two properties that you need from your entity
    return new Widget(){ PropertyA = widget.PropetyA, PropertyB = widget.PropertyB };

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2015 5:09 PM
  • User281315223 posted

    want to use the techniques you provided to get the contact's employer from the vendor entity based on the VendorID, but I'm not certain where this should reside.

    As long as you are in the context of your data entities context (i.e. within the using statement), you should be be able to use a previously accessed entity to perform another query :

    using(var context = new DatabaseEntities())
    {
         // Get the first widget
         var widget = context.Widgets.FirstOrDefault();
    
         // If this widget exists, perform another query
         if(widget != null)
         {
               // Get the first invoice for that widget
               var invoice = context.Invoices.FirstOrDefault(i => i.WidgetId = widget.Id);
         }
    }

    It seems like the code that gets this vendor name using the vendor Id should reside in the contact controller. 

    Yes, generally you'll be performing your data access within your Controller (unless you build a specific service or repository that handles wrapping these calls, in which case you would just call those from your Controller).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2015 10:14 AM

All replies

  • User281315223 posted

    Generally you would want to use your specific entities to only pull out what you need from the database (and not instantiate the context each time). For instance if you needed to access a Widget entity, you might use something like this :

    using(var context = new DatabaseEntities())
    {
         // Get the first widget
         var widget = context.Widgets.FirstOrDefault();
    }

    Likewise, if you didn't want to actually pass across all of your data, you could always use a Select() method to only populate the properties you need into an object or another class :

    // Get the first widget
    var widget = context.Widgets.FirstOrDefault();
    
    // Return a Widget instance that only has two properties that you need from your entity
    return new Widget(){ PropertyA = widget.PropetyA, PropertyB = widget.PropertyB };

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2015 5:09 PM
  • User-1833980242 posted

    Thanks for the help!

    I would like to know where this should reside if it's getting data from another entity that is used in the current entity.  For example, there is an VendorID stored in the contact table.  I want to use the techniques you provided to get the contact's employer from the vendor entity based on the VendorID, but I'm not certain where this should reside.  It seems like the code that gets this vendor name using the vendor Id should reside in the contact controller. 

    Is this correct? 

    Friday, August 28, 2015 10:03 AM
  • User281315223 posted

    want to use the techniques you provided to get the contact's employer from the vendor entity based on the VendorID, but I'm not certain where this should reside.

    As long as you are in the context of your data entities context (i.e. within the using statement), you should be be able to use a previously accessed entity to perform another query :

    using(var context = new DatabaseEntities())
    {
         // Get the first widget
         var widget = context.Widgets.FirstOrDefault();
    
         // If this widget exists, perform another query
         if(widget != null)
         {
               // Get the first invoice for that widget
               var invoice = context.Invoices.FirstOrDefault(i => i.WidgetId = widget.Id);
         }
    }

    It seems like the code that gets this vendor name using the vendor Id should reside in the contact controller. 

    Yes, generally you'll be performing your data access within your Controller (unless you build a specific service or repository that handles wrapping these calls, in which case you would just call those from your Controller).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2015 10:14 AM