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

查看:367
本文介绍了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

整个错误消息:


错误CS1061'ModelDBContext'不包含'Configuration'的定义并且没有可访问的扩展名可以找到接受 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/zh-cn/ef/core/querying/related-data ,从EF Core 2.1开始,有一种方法可以启用带或不带代理的延迟加载

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。确保将导航属性定义为虚拟

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。没有代理的延迟加载:

a。如实体类型构造器中所述,将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天全站免登陆