entity-framework - .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; }
}
这是我收到的结果,我想删除null
POS 的帐户
[{
"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
推荐阅读
- javascript - 纽约时报的对象阅读以在 HTML 中绘制文章表
- python - 将 URL 路由到 GAE Flex 服务器,而站点的其余部分在 GAE 标准上运行
- android - 不会在 shouldOverrideUrlLoading 中捕获重定向
- angular - 这是在 angularfire2 中使用 rxjs MergeMap 的正确方法吗?
- javascript - 如何在后端数据发生更改时发出警报?
- intrusion-detection - 我可以让 suricata 听 localhost
- heroku - Heroku 域 - 我自己的域作为后缀添加到 heroku 域的末尾
- maps - 从当前位置到 Google Maps API 的任何替代方案?
- r - 如何将矩阵行转换为零,除了r中的最后一行
- android - 科尔多瓦 fcm 使用 firebase 功能推送通知问题