.NET Framework:如何使用代码优先约定在 EF6 中禁用延迟加载?

.NET Framework : how to disable lazy loading in EF6 with code first convention?

请解释一下,如何使用代码优先约定在 EF6 中禁用延迟加载选项?请注意,我已经尝试在 DbContext 中为所有使用虚拟导航 属性 的实体禁用它。

另请注意,我使用 Code First 方法生成了数据库,并且我将延迟加载选项设置为 false,并将代理创建也设置为 false

我的DbContext配置

{
    public MyContext():base("name=MaChaine")
    {
        this.Configuration.LazyLoadingEnabled = false;
        this.Configuration.ProxyCreationEnabled = false;
    }

    public DbSet<Account> accounts { get; set; }
    public DbSet<deposit> deposits { get; set; }
    public DbSet<Withdraw> withdraws { get; set; }
    public DbSet<Event> events { get; set; }
    public DbSet<import_history> import_Histories { get; set; }
    public DbSet<POS>  points_of_sales { get; set; }
    public DbSet<Ticket> tickets { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //config + customconventions
        // one to many : Point of sales can have many accounts but an account can have one only point of sale 
        modelBuilder.Entity<Account>()
            .HasRequired<POS>(a => a.POS)
            .WithMany(p => p.accounts)
            .HasForeignKey<int>(a => a.id_pos)
            .WillCascadeOnDelete(false);
        // one to many : an account can have many tickets but ticket can have one only account
        modelBuilder.Entity<Ticket>()
            .HasRequired<Account>(t2 => t2.Account)
            .WithMany(a2 => a2.Tickets)
            .HasForeignKey<int>(t2 => t2.n_acccount)
            .WillCascadeOnDelete(false);
        // one to many : an account can have many deposit transactions but deposit are for one only account 
        modelBuilder.Entity<deposit>()
            .HasRequired<Account>(d => d.Account)
            .WithMany(a3 => a3.deposits)
            .HasForeignKey<int>(d => d.Id_Account)
            .WillCascadeOnDelete(false);
        // one to many ; account can have many withdraws but a withdraw can only be for one account
        modelBuilder.Entity<Withdraw>()
            .HasRequired<Account>(w => w.Account)
            .WithMany(a4 => a4.Withdraws)
            .HasForeignKey<int>(w => w.N_account)
            .WillCascadeOnDelete(false);
    }
}

排名 class :

public class POS
{
    public int ID { get; set; }
    public string Code_pos { get; set; }
    public string r_social { get; set; }
    public string nom_commercial { get; set; }
    public int n_tva { get; set; }
    public string t_pos { get; set; }
    public string mode_op { get; set; }
    public string nom { get; set; }
    public string prenom { get; set; }
    public string phone { get; set; }
    public string email { get; set; }
    public string exploitant { get; set; }
    public string depends_on { get; set; }
    public string status { get; set; }
    public DateTime date_creation { get; set; }
    public DateTime date_change_status { get; set; }
    public string type_contract { get; set; }

    public virtual ICollection<Account> accounts { get; set; }
}

帐号class:

public class Account
{
    public int ID { get; set; }
    public int n_account { get; set; }
    public string pseudo { get; set; }
    public string nom { get; set; }
    public string prenom { get; set; }
    public DateTime date_birth { get; set; }
    public int cin { get; set; }
    public string mobile { get; set; }
    public string statut { get; set; }
    public DateTime date_creation { get; set; }
    public string email { get; set; }
    public int id_pos { get; set; }
    public POS POS { get; set; }

    public virtual ICollection<deposit> deposits { get; set; }
    public virtual ICollection<Withdraw> Withdraws { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }     
}

这是我收到的结果,我想删除 POS 的 null 帐户

[{
    "ID": 1,
    "Code_pos": "dfd",
    "r_social": "testpos",
    "nom_commercial": "testpos",
    "n_tva": 2,
    "t_pos": "testpos",
    "mode_op": "testpos",
    "nom": "testpos",
    "prenom": "testpos",
    "phone": "testpos",
    "email": "testpos",
    "exploitant": "testpos",
    "depends_on": "testpos",
    "status": "testpos",
    "date_creation": "1990-02-02T00:00:00",
    "date_change_status": "1990-02-02T00:00:00",
    "type_contract": "type",
    "accounts": null
}]

问题已修复,这不是延迟加载而是 Json 显示导航 属性 的序列化问题,已通过在 API 的 Webapiconfig 中添加以下代码解决项目

            config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;

感谢@DavidBrowne-Microsoft

解决问题的来源也在下面: https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization