简体   繁体   中英

VB.NET Entity Framework Does context load all records when it's created?

I'm an absolute beginner with entity framework. I recently found ef and I think it will do a fine job with my project. I have played with it and I am a little confused with how it actually works.

Here's the situation.

Contact table has id, fname, lname, phone columns. And if I run the code below one record will be inserted into the Contact table.

Contact con = new Contact();
con.fname = TextBox1.Text;
con.lname = TextBox2.Text;
con.phone = TextBox3.Text;

ContactsDb db = new ContactsDb();
db.Contacts.AddObject(con);
db.SaveChanges();

My question is that when ContactsDb db is created, does it load all the records from the table then AddObject adds the new contact to that list?

Or when I call AddObject it just adds the new contact to an empty list and SaveChanges will save contacts on that list?

I hope I'm describing enough as it's my first time asking a question.

The reason why I'm worrying is that as the database becomes bigger, it will take more time to load all the records from tables and if DB context does load all when it is created, the performance would become a problem.

Thanks in advance

Entity framework uses deferred loading, meaning the data is loaded only when specifically requested. The db context object you create only acts as an interface to the database. Any action you make on the context, like adding an object, is only applied to the database when calling .SaveChanges() on the context. Entity Framework then devises a query to insert or retrieve the data, and maps any retrieved objects to relational entities you can consume in code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM