EF Core 中 .Configuration.ProxyCreationEnabled 的等价物是什么? [英] What is the equivalent of .Configuration.ProxyCreationEnabled in EF Core?

查看:35
本文介绍了EF Core 中 .Configuration.ProxyCreationEnabled 的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Entity Framework Core 中.Configuration 的等价物是什么?接收错误如下

What is the equivalent of .Configuration in Entity Framework Core? Receiving error below

代码示例:

        List<DocumentStatus> documentStatuses;
        using (var db = new ModelDBContext())
        {
            db.Configuration.ProxyCreationEnabled = false;
            documentStatuses = db.DocumentStatus.ToList();
        }


        using (var db = new ModelDBContext())
        {
            db.Configuration.ProxyCreationEnabled = false;
            //Expression<Func<Owner, bool>> predicate = query => true;

db.Configuration.ProxyCreationEnabled

db.Configuration.ProxyCreationEnabled

错误信息:

错误 CS1061ModelDBContext"不包含配置"的定义,并且找不到接受ModelDBContext"类型的第一个参数的可访问扩展方法配置"(您是否缺少 using 指令或程序集引用?)

Error CS1061 'ModelDBContext' does not contain a definition for 'Configuration' and no accessible extension method 'Configuration' accepting a first argument of type 'ModelDBContext' could be found (are you missing a using directive or an assembly reference?)

推荐答案

基于 Entity Framework Core 文档:https://docs.microsoft.com/en-us/ef/core/querying/related-data,从 EF Core 2.1 开始,有一种方法可以启用 Lazy使用或不使用代理加载.

Based on Entity Framework Core docs: https://docs.microsoft.com/en-us/ef/core/querying/related-data, from EF Core 2.1, there is a way to enable Lazy Loading with or without proxy.

1.使用代理延迟加载:

一个.确保您的导航属性定义为虚拟"

a. Make sure your navigation property are defined as "virtual"

B.安装 Microsoft.EntityFrameworkCore.Proxies 包

b. Install the Microsoft.EntityFrameworkCore.Proxies package

c.通过调用 UseLazyLoadingProxies 启用它

c. Enable it with a call to UseLazyLoadingProxies

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseLazyLoadingProxies()
        .UseSqlServer(myConnectionString);

或者在使用 AddDbContext 时启用它

Or enable it when using AddDbContext

.AddDbContext<BloggingContext>(
    b => b.UseLazyLoadingProxies()
          .UseSqlServer(myConnectionString));

2.没有代理的延迟加载:

一个.将 ILazyLoader 服务注入实体,如实体类型构造函数中所述.例如:

a. Injecting the ILazyLoader service into an entity, as described in Entity Type Constructors. For example:

public class Blog
{
    private ICollection<Post> _posts;

    public Blog()
    {
    }

    private Blog(ILazyLoader lazyLoader)
    {
        LazyLoader = lazyLoader;
    }

    private ILazyLoader LazyLoader { get; set; }

    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Post> Posts
    {
        get => LazyLoader.Load(this, ref _posts);
        set => _posts = value;
    }
}

<小时>

默认情况下,EF Core 不会使用带有代理的延迟加载,但如果您想使用代理,请遵循第一种方法.

这篇关于EF Core 中 .Configuration.ProxyCreationEnabled 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆