首页 > 解决方案 > .NET Framework:如何使用代码优先约定禁用 EF6 中的延迟加载?

问题描述

请解释一下,如何使用代码优先约定禁用 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);
    }
}

POS类:

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; }
}

账户类:

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; }     
}

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

[{
    "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
}]

标签: entity-framework.net-4.0

解决方案


问题已修复,不是延迟加载,而是显示导航属性的 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


推荐阅读