In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "Entity Framework Core deferred loading method how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this article "Entity Framework Core delayed loading method how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
It is well known that lazy loading (Lazy Loading) is supported in EF 6 and previous versions, but it is not supported in EF Core, so the Include method must be used to support data loading of navigation properties. For now, however, the EF Core development team plans to restore support for this feature (it hasn't been released yet, but it can be downloaded and tested on Github).
Lazy loading
Lazy loading can also be called demand loading or delayed loading. It can be understood in two aspects: on the one hand, it means that the data is not needed temporarily, it does not need to be loaded immediately, but can be postponed until it is used; on the other hand, it means that it is not sure whether the data will be needed, so please do not load it for the time being. don't load it until you're sure you need it. Lazy loading is a very important data access feature, which can effectively reduce the interaction with the data source (note that the interaction mentioned here does not refer to the number of interactions, but to the amount of data), so as to improve the performance of the program.
EF 6 lazy load
Let's first take a look at how lazy loading is used in EF 6.
Entity definition:
Public class Order {public int OrderID {get; set;} public string CustomerID {get; set;} public DateTime? OrderDate {get; set;} public virtual ICollection OrderDetails {get; set;}} public class OrderDetail {public int OrderID {get; set;} public int ProductID {get; set;} public decimal UnitPrice {get; set;} public short Quantity {get; set;} public float Discount {get; set;} public virtual Order Order {get; set;}}
Here we define the order, the order detail entity, which is an one-to-many relationship and is associated through the OrderId field.
Using (NorthwindContext context = new NorthwindContext ()) {Order order = await context.Orders.SingleAsync (item = > item.OrderID = = 10253); Assert.NotNull (order); Assert.NotNull (order.OrderDetails); Assert.Equal (3, order.OrderDetails.Count);}}
After querying the order number 10253, if we need to access the details of the order and do not need to write the code for another data query, we can access the navigation properties directly. EF will automatically fill in the values of the attributes for us.
Lazy loading needs to pay attention to the following two points:
Lazy loading is enabled in the configuration (on by default)
An entity class cannot be a sealed class, and the navigation property must be a virtual attribute.
Enable lazy loading in EF Core
Lazy loading is not supported in the latest version of EF Core, and developers must use the Include method to complete the loading of navigation properties.
Using (NorthwindContext context = new NorthwindContext ()) {Order order = await context.Orders.Include (e = > e.OrderDetails) .SingleAsync (item = > item.OrderID = = 10253); Assert.NotNull (order); Assert.NotNull (order.OrderDetails); Assert.Equal (3, order.OrderDetails.Count);}
You need to download the latest source code on Github to test this feature aspnet/EntityFrameworkCore.
Enable lazy loading:
Public class NorthwindContext: DbContext {protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) {var sqlConnectionStringBuilder = new SqlConnectionStringBuilder {DataSource = "*", InitialCatalog = "Northwind", UserID = "sa", Password = "sa"}; optionsBuilder.UseSqlServer (sqlConnectionStringBuilder.ConnectionString) OptionsBuilder.UseLazyLoadingProxies (); base.OnConfiguring (optionsBuilder);}}
To use it in a normal application, simply add a call to the UseLazyLoadingProxies () extension method to the OnConfiguring method of DbContext.
The framework currently uses the Castle.Core framework to generate proxy classes to achieve delayed loading of navigation properties, which the development team intends to use as an optional installation package for EF Core.
After reading this, the article "how to use Entity Framework Core deferred loading method" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.