DbSet <>和虚拟DbSet <>有什么区别?

mag*_*gos 36 entity-framework ef-code-first

在实体框架代码中,当我声明实体时,我必须使用DbSet <>类型的属性.例如:

public DbSet<Product> Products { get; set; }
public DbSet<Customer> Customers { get; set; }
Run Code Online (Sandbox Code Playgroud)

最近我遇到了声称为虚拟的DbSet <>.

public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
Run Code Online (Sandbox Code Playgroud)

有什么不同?启用了哪些EF功能?

Yul*_*dra 24

public class AppContext : DbContext
{
    public AppContext()
    {
        Configuration.LazyLoadingEnabled = true;
    }

    public virtual DbSet<AccountType> AccountTypes { get; set; }
}

public class AccountType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<AccountCode> AccountCodes { get; set; }
}

public class AccountCode
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid AccountTypeId { get; set; }
    public virtual AccountType AccountType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

导航属性上的virtual关键字用于启用延迟加载机制,但必须启用配置的LazyLoadingEnabled属性.

AccountType :: AccountCodes导航属性上的虚拟关键字将在db上下文仍处于活动状态时以编程方式访问该属性时加载所有帐户代码.

using (var context = new AppContext())
{
    var accountType = context.AccountTypes.FirstOrDefault();
    var accountCodes = accountType.AccountCodes;
}
Run Code Online (Sandbox Code Playgroud)

虽然派生的DbContext类(虚拟DbSet <>)上的virtual关键字用于测试目的(模拟DbSet属性),但在这种情况下,虚拟关键字与延迟加载无关.

=====更新=====

通常我们正在对服务/逻辑进行测试,例如我们为帐户类型服务提供了另一个层,如下所示.并且该服务通过构造函数使用某种依赖注入接受db上下文实例.

public class AccountTypeService
{
    public AppContext _context;

    public AccountTypeService(AppContext context)
    {
        _context = context;
    }

    public AccountType AddAccountType(string name)
    {
        var accountType = new AccountType { Id = Guid.NewGuid(), Name = name };
        _context.AccountTypes.Add(accountType);
        _context.SaveChanges();
        return accountType;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我们需要测试帐户类型服务,在这种情况下我使用mstest和automoq来创建模拟类.

[TestClass]
public class AccountTypeServiceTest
{
    [TestMethod]
    public void AddAccountType_NormalTest()
    {
        // Arranges.
        var accountTypes = new List<AccountType>();
        var accountTypeSetMock = new Mock<DbSet<AccountType>>();
        accountTypeSetMock.Setup(m => m.Add(It.IsAny<AccountType>())).Callback<AccountType>(accountType => accountTypes.Add(accountType));

        var appContextMock = new Mock<AppContext>();
        appContextMock.Setup(m => m.AccountTypes).Returns(accountTypeSetMock.Object);
        var target = new AccountTypeService(appContextMock.Object);

        // Acts.
        var newAccountType = target.AddAccountType("test");

        // Asserts.
        accountTypeSetMock.Verify(m => m.Add(It.IsAny<AccountType>()), Times.Once());
        appContextMock.Verify(m => m.SaveChanges(), Times.Once());
        Assert.AreEqual(1, accountTypes.Count);
        Assert.IsNotNull(newAccountType);
        Assert.AreNotEqual(Guid.Empty, newAccountType.Id);
        Assert.AreEqual("test", newAccountType.Name);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

请注意,在EF Core(当前为1.0和2.0)中,仍不支持LazyLoading方案,因此使用“虚拟”或不使用“虚拟”都没有什么不同。

仅供参考。脚手架生成“虚拟”关键字可能在将来的EF Core版本中支持LazyLoading技术!

在EF Core 2.1中,开发团队添加了对LazyLoading的支持。更多信息在这里

  • 基本虚拟意味着它可以在派生类中被覆盖。 (2认同)