DMVC
DMVC

Reputation: 240

Entity Framework "Basic" Doubts

Just started to experiment EF, and some doubts came across:

  1. Suppose I've got a DB table with 10 000 rows. When the EF entity is instantiated in my source, do I have these 10 000 rows in memory?
  2. Does the lazy instantiation mechanism has some kind of effect on the above?
  3. It's been my practice to include some audit fields on my DB tables, such as CreateOn and ChangedOn. These are DateTime and I use to set its values to GETDATE() (SQL Server time). EF allows changes to entity properties to occur either on the client machine (desktop apps) or IIS. Is there any way to set these values on the SQL Server?

Thanks.

Upvotes: 0

Views: 77

Answers (3)

Omar.Alani
Omar.Alani

Reputation: 4130

  1. If your database is huge and has 10,000 tables then you should avoid having a single context as this will be initialized and loaded with each initialization to the context and will be in memory, so you should avoid this by using bounded contexts where you divide your tables into logical areas for example financial context, security context, etc and each context will define only the tables that context is dealing with which leads to lighter initialization and faster processing. See this link http://msdn.microsoft.com/en-us/magazine/jj883952.aspx

  2. About created date, I do the same and I define an Interfaces called ITrackable that has the createddate property and also modifieddate and implement this interface by all the entities that have these properties and this allows me to set those field before I do ef saving process by looping through all the tracked entities of type ITrackable and set the created date and modified date properties.

Hope that helps.

Upvotes: 0

Fka
Fka

Reputation: 6234

  1. When you instantiate entity framework, no data is obtained. There is only set connection via ADO.NET (connection is added to pool, etc.).
  2. Lazy loading allow entities to be loaded when needed, it means before that moment, they aren't obtained from database. For example (http://msdn.microsoft.com/en-us/library/vstudio/dd456846(v=vs.100).aspx):

    using (AdventureWorksEntities context =new AdventureWorksEntities())
    {
    
        // You do not have to set context.ContextOptions.LazyLoadingEnabled to true 
        // if you used the Entity Framework to generate the object layer.
        // The generated object context type sets lazy loading to true
         // in the constructor. 
        context.ContextOptions.LazyLoadingEnabled = true;
    
        // Display ten contacts and select a contact
        var contacts = context.Contacts.Take(10);
        foreach (var c in contacts)
            Console.WriteLine(c.ContactID);
    
        Console.WriteLine("Select a customer:");
        Int32 contactID = Convert.ToInt32(Console.ReadLine());
    
        // Get a specified customer by contact ID. 
        var contact = context.Contacts.Where(c => c.ContactID == contactID).FirstOrDefault();
    
        // If lazy loading was not enabled no SalesOrderHeaders would be loaded for the contact.
        foreach (SalesOrderHeader order in contact.SalesOrderHeaders)
        {
            Console.WriteLine("SalesOrderID: {0} Order Date: {1} ",
                order.SalesOrderID, order.OrderDate);
        }
    }
    
  3. Answer is here: https://stackoverflow.com/a/5965620/1617002

Upvotes: 0

krillgar
krillgar

Reputation: 12805

1) Only the entities that match your query are loaded into memory, and that only occurs as the list is iterated. (thanks to yield, unless you do a .ToList())

2) Lazy instantiation means that the complex properties of your objects are only fetched from the database as you use them (unless you have an .Include()).

3) You can use triggers, for UpdatedOn fields, or Defaults for CreatedOn fields in your SQL Server database. Don't include those properties in your Code First entities, and you'll be fine (unless you want to use them for any sort of querying in your C#).

Upvotes: 1

Related Questions