Entity Framework Code First实体关联数据加载

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03



  在项目过程中,两个实体数据之间在往往并非完全独立的,而是存在一定的关联关系,如一对一、一对多及多对多等关联。存在关联关系的实体,经常根据一个实体的实例来查询获取与之关联的另外实体的实例。

  Entity Framework常用处理数据关联加载的方式有3种:延迟加载(Lazy Loading)、贪婪加载(Eager Loading)以及显示加载(Explicit Loading)。

  1、延迟加载(Lazy Loading)

  延迟加载是项目应用中常见的方式,Entity Framework在需要时可以自动为一个实体的实例获取关联的数据。

  Entity Framework自动延迟加载需要满足的条件:

  1>、POCO类必须是public而非sealed;

  2>、集合属性必须的Virtual修饰的,这样Entity Framework才能Override以包含延迟加载的逻辑。

  示例:

  文件类Province.cs:

using System;
using System.Collections.Generic;

namespace Portal.Models
{
    public class Province
    {
        public Province()
        {
            this.Cities = new List<City>();
        }

        public int ProvinceID { get; set; }
        public string ProvinceNo { get; set; }
        public string ProvinceName { get; set; }
        public virtual ICollection<City> Cities { get; set; }
    }
}

  文件类City.cs:

using System;
using System.Collections.Generic;

namespace Portal.Models
{
    public class City
    {
        public int CityID { get; set; }
        public Nullable<int> ProvinceID { get; set; }
        public string CityNo { get; set; }
        public string CityName { get; set; }
        public virtual Province Province { get; set; }
    }
}

  文件类Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;

using Portal.Models;

namespace Portal
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new PortalContext())
            {
                var province = ctx.Provinces.Find(3);

                foreach (var city in province.Cities)
                {
                    Console.WriteLine(city.CityName);
                }
            }
        }
    }
}

  以上代码在运行之后,执行了两条SQL语句,分别用于读取单条Province记录及与该条记录相关联的City记录。

exec sp_executesql N‘SELECT
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[ProvinceNo] AS [ProvinceNo],
[Limit1].[ProvinceName] AS [ProvinceName]
FROM ( SELECT TOP (2)
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[ProvinceNo] AS [ProvinceNo],
    [Extent1].[ProvinceName] AS [ProvinceName]
    FROM [dbo].[Province] AS [Extent1]
    WHERE [Extent1].[ProvinceID] = @p0
)  AS [Limit1]‘,N‘@p0 int‘,@p0=3
exec sp_executesql N‘SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=3

  延迟加载的不足:

  延迟加载使用简单,应用程序不需要真正知道数据已经被从数据库中加载出来,但只要将可能导致大量的SQL查询被发送到数据库中执行,数据库进行了不必要的查询。

  2、贪婪加载(Eager Loading)

  贪婪加载:使用Include加载关联的数据,在Entity Framework进行查询时,即同时加载出关联的数据。Entity Framework贪婪加载将使用一条JOIN的SQL语句进行查询。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;

using Portal.Models;

namespace Portal
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new PortalContext())
            {
                var provinces = ctx.Provinces
                    .Include(p => p.Cities);

                foreach (var province in provinces)
                {
                    foreach (var city in province.Cities)
                    {
                        Console.WriteLine("{0}-{1}", province.ProvinceName, city.CityName);
                    }
                }
            }
        }
    }
}

  运行代码所执行的SQL语句:

SELECT
[Project1].[ProvinceID] AS [ProvinceID],
[Project1].[ProvinceNo] AS [ProvinceNo],
[Project1].[ProvinceName] AS [ProvinceName],
[Project1].[C1] AS [C1],
[Project1].[CityID] AS [CityID],
[Project1].[ProvinceID1] AS [ProvinceID1],
[Project1].[CityNo] AS [CityNo],
[Project1].[CityName] AS [CityName]
FROM ( SELECT
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[ProvinceNo] AS [ProvinceNo],
    [Extent1].[ProvinceName] AS [ProvinceName],
    [Extent2].[CityID] AS [CityID],
    [Extent2].[ProvinceID] AS [ProvinceID1],
    [Extent2].[CityNo] AS [CityNo],
    [Extent2].[CityName] AS [CityName],
    CASE WHEN ([Extent2].[CityID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM  [dbo].[Province] AS [Extent1]
    LEFT OUTER JOIN [dbo].[City] AS [Extent2] ON [Extent1].[ProvinceID] = [Extent2].[ProvinceID]
)  AS [Project1]
ORDER BY [Project1].[ProvinceID] ASC, [Project1].[C1] ASC

  Include语句可以在一次查询中使用多次。

ctx.Categories
    .Include(c => c.Products)
    .Include(c => c.News);

  贪婪加载的不足:

  贪婪加载的优势在于仅执行1次SQL查询即返回所需要的结果。但使用JOIN查询在数据库记录条数较多时,多条简单的SQL查询往往比一条复杂的JOIN查询效率要好。

  使用Include的LINQ查询

var provinces = ctx.Provinces
    .Include(p => p.Cities)
    .Where(p => p.ProvinceID > 10);
var provinces = from p in ctx.Provinces.Include(p => p.Cities)
                where p.ProvinceID > 10
                select p;
var expr = from p in ctx.Provinces
           where p.ProvinceID > 10
           select p;
var provinces = expr.Include(p => p.Cities);

  3、显示加载(Explicit Loading)

  显示加载与延迟加载一样,采用主数据与关联数据独立分开加载。显示加载与延迟加载的区别在于显示加载不会自动的加载关联数据,需要调用方法去加载。

  显示加载是使用DbContext.Entry方法来实现的,Entry方法可以获取DbContext中的实体信息。在使用Entry获取实体信息之后,可以使用Collection或Reference方法获取和操作实体关联的集合属性。如使用Load方法查询集合属性。

  示例1:显示加载,使用Collection获取集合属性

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(3);
    ctx.Entry(province)
        .Collection(p => p.Cities)
        .Query()
        .Load();

    foreach (var city in province.Cities)
    {
        Console.WriteLine("{0}-{1}", province.ProvinceName, city.CityName);
    }
}

  上面的代码运行之后,执行的SQL语句:

exec sp_executesql N‘SELECT
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[ProvinceNo] AS [ProvinceNo],
[Limit1].[ProvinceName] AS [ProvinceName]
FROM ( SELECT TOP (2)
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[ProvinceNo] AS [ProvinceNo],
    [Extent1].[ProvinceName] AS [ProvinceName]
    FROM [dbo].[Province] AS [Extent1]
    WHERE [Extent1].[ProvinceID] = @p0
)  AS [Limit1]‘,N‘@p0 int‘,@p0=3
exec sp_executesql N‘SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=3

  从代码运行所执行的SQL语句可以看出,其查询数据库的方式与延迟加载是相同的。

  示例2:显示加载,使用Reference方法获取引用属性

using (var ctx = new PortalContext())
{
    var city = ctx.Cities.Find(10);
    ctx.Entry(city).Reference(c => c.Province);
    Console.WriteLine("{0}-{1}", city.Province.ProvinceName, city.CityName);
}

  上面的代码运行之后执行的SQL语句:

exec sp_executesql N‘SELECT
[Limit1].[CityID] AS [CityID],
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[CityNo] AS [CityNo],
[Limit1].[CityName] AS [CityName]
FROM ( SELECT TOP (2)
    [Extent1].[CityID] AS [CityID],
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[CityNo] AS [CityNo],
    [Extent1].[CityName] AS [CityName]
    FROM [dbo].[City] AS [Extent1]
    WHERE [Extent1].[CityID] = @p0
)  AS [Limit1]‘,N‘@p0 int‘,@p0=10
exec sp_executesql N‘SELECT
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[ProvinceNo] AS [ProvinceNo],
[Extent1].[ProvinceName] AS [ProvinceName]
FROM [dbo].[Province] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=3

  检查集合属性是否已经加载:

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(3);
    Console.WriteLine("Before load:{0}", ctx.Entry(province).Collection(p => p.Cities).IsLoaded);

    ctx.Entry(province)
        .Collection(p => p.Cities)
        .Load();

    Console.WriteLine("After load:{0}", ctx.Entry(province).Collection(p => p.Cities).IsLoaded);
}

  4、集合属性查询

  在使用Entry和Collection方法获取到实体集合属性之后,可以使用Query方法对集合属性进行查询。

  示例:从内存中查询集合属性

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(5);
    var cities = from c in province.Cities
                 where c.CityID > 30
                 select c;
    foreach (var city in cities)
    {
        Console.WriteLine("{0}-{1}", city.CityID, city.CityName);
    }
}

  代码运行之后执行的SQL语句:

exec sp_executesql N‘SELECT
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[ProvinceNo] AS [ProvinceNo],
[Limit1].[ProvinceName] AS [ProvinceName]
FROM ( SELECT TOP (2)
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[ProvinceNo] AS [ProvinceNo],
    [Extent1].[ProvinceName] AS [ProvinceName]
    FROM [dbo].[Province] AS [Extent1]
    WHERE [Extent1].[ProvinceID] = @p0
)  AS [Limit1]‘,N‘@p0 int‘,@p0=5
exec sp_executesql N‘SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE [Extent1].[ProvinceID] = @EntityKeyValue1‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=5

  从对City表执行的SQL语句可以看出,其并对加入查询条件,仅只是对之前通过延长加载方式将Province实体的Cities集合属性载人到内存中,然后通过对内存中的Cities数据进行内存查询,并未生成新的包含查询条件的SQL语句。

  示例:在数据库中查询集合属性

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(5);
    var expr = ctx.Entry(province)
        .Collection(p => p.Cities)
        .Query();
    var cities = from c in expr
                 where c.CityID > 30
                 select c;
    foreach (var city in cities)
    {
        Console.WriteLine("{0}-{1}", city.CityID, city.CityName);
    }
}

  代码运行之后执行的SQL语句:

exec sp_executesql N‘SELECT
[Limit1].[ProvinceID] AS [ProvinceID],
[Limit1].[ProvinceNo] AS [ProvinceNo],
[Limit1].[ProvinceName] AS [ProvinceName]
FROM ( SELECT TOP (2)
    [Extent1].[ProvinceID] AS [ProvinceID],
    [Extent1].[ProvinceNo] AS [ProvinceNo],
    [Extent1].[ProvinceName] AS [ProvinceName]
    FROM [dbo].[Province] AS [Extent1]
    WHERE [Extent1].[ProvinceID] = @p0
)  AS [Limit1]‘,N‘@p0 int‘,@p0=5
exec sp_executesql N‘SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE ([Extent1].[ProvinceID] = @EntityKeyValue1) AND ([Extent1].[CityID] > 30)‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=5

  集合属性Count查询

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(5);
    var expr = ctx.Entry(province)
        .Collection(p => p.Cities)
        .Query();
    Console.WriteLine(expr.Count());
}

  代码运行生成的SQL语句:

exec sp_executesql N‘SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
    COUNT(1) AS [A1]
    FROM [dbo].[City] AS [Extent1]
    WHERE [Extent1].[ProvinceID] = @EntityKeyValue1
)  AS [GroupBy1]‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=5

  显示加载集合属性的子集:

using (var ctx = new PortalContext())
{
    var province = ctx.Provinces.Find(5);
    ctx.Entry(province)
        .Collection(p => p.Cities)
        .Query()
        .Where(c => c.CityNo.Contains("3"))
        .Load();
}

  代码运行后生成的SQL语句:

exec sp_executesql N‘SELECT
[Extent1].[CityID] AS [CityID],
[Extent1].[ProvinceID] AS [ProvinceID],
[Extent1].[CityNo] AS [CityNo],
[Extent1].[CityName] AS [CityName]
FROM [dbo].[City] AS [Extent1]
WHERE ([Extent1].[ProvinceID] = @EntityKeyValue1) AND ([Extent1].[CityNo] LIKE N‘‘%3%‘‘)‘,N‘@EntityKeyValue1 int‘,@EntityKeyValue1=5

参考页面:http://qingqingquege.cnblogs.com/p/5933752.html

时间: 2024-09-30 14:15:39

Entity Framework Code First实体关联数据加载的相关文章

Entity Framework Code First学习系列

Entity Framework Code First学习系列目录 Entity Framework Code First学习系列说明:开发环境为Visual Studio 2010 + Entity Framework 5.0+MS SQL Server 2012,在数据库方面Entity Framework Code First在Entity Framework 5.0仅支持MS SQL Server数据库.在接下来的随笔中,均使用项目名称为Portal的控制台应用程序为例.具体的系统学习目

浅谈Entity Framework中的数据加载方式

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 如果你还没有接触过或者根本不了解什么是Entity Framework,那么请看这里http://www.entityframeworktutorial.net/EntityFramework-Architecture.aspx,其中的一系列文

Entity Framework Code First数据库连接

参考页面: http://www.yuanjiaocheng.net/entity/entitytypes.html http://www.yuanjiaocheng.net/entity/entity-relations.html http://www.yuanjiaocheng.net/entity/entity-lifecycle.html http://www.yuanjiaocheng.net/entity/code-first.html http://www.yuanjiaochen

Entity Framework Code First关系映射约定

Entity Framework Code First关系映射约定 本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个不同表之间可以存在外键依赖关系,一个表自身也可以有自反关系(表中的一个字段引用主键,从而也是外键字段). Entity Framework Code First默认多重关系的一些约定规则: 一对多关系:两个类中分别包含一个引用和一个

Entity Framework Code First主外键关系映射约定

本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个不同表之间可以存在外键依赖关系,一个表自身也可以有自反关系(表中的一个字段引用主键,从而也是外键字段). Entity Framework Code First默认多重关系的一些约定规则: 一对多关系:两个类中分别包含一个引用和一个集合属性,也可以是一个类包含另一个类的引用属性,或一个类包含另一个类

Entity Framework Code First关系映射约定【l转发】

本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个不同表之间可以存在外键依赖关系,一个表自身也可以有自反关系(表中的一个字段引用主键,从而也是外键字段). Entity Framework Code First默认多重关系的一些约定规则: 一对多关系:两个类中分别包含一个引用和一个集合属性,也可以是一个类包含另一个类的引用属性,或一个类包含另一个类

ADO.NET Entity Framework -Code Fisrt 开篇(一)

ADO.NET Entity Framework -Code Fisrt 开篇(一) 2012-12-25 15:13 by 易code, 911 阅读, 0 评论, 收藏, 编辑 ADO.NET Entity Framework 是微软的一套实体映射框架.发布EF4.1(Entity Framework )时,又提出了代码先行的设计理念(the code comes first, the rest follows).具体好处哪是多多,查资料吧. 参考资料:Programming Entity

MVC5 Entity Framework学习之读取相关数据

前一篇文章中完成了School 数据模型,接下来你将学习如何读取和显示相关的数据--这里指Entity Framework加载至导航属性中的数据. 下图是完成后的效果图 延迟.预先和显示加载相关数据 Entity Framework可以通过多种方法向实体的导航属性中加载数据 延迟加载(Lazy loading) 当实体第一次被读取时,相关数据并不会被检索.但是,当你第一次访问导航属性时,该导航属性所需的数据会自动加载.这是向数据库发送多个查询语句的结果--一次是读取实体本身,接着是每次与被检索的

Entity Framework Code First (三)Data Annotations

Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C