use*_*310 0 .net c# entity-framework-core asp.net-core
除非我使用 ,否则我无法自动填充导航属性Include()。如何在没有 的情况下自动填充导航属性Include()?
我使用的是 EF core 5.0
Class customer
{
[key]
public int MappingID { get; set; }
public string MappingName {get;set;}
}
Class Order
{
[key]
public int orderID {get;set;}
public string name {get;set;}
public int MappingID { get; set; }
[ForeignKey(nameof(MappingID))]
public virtual customer customerMapping { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是行不通的。customerMapping返回为null-
_context.Order.FirstOrDefault(x => x.orderID == 1);
Run Code Online (Sandbox Code Playgroud)
这有效。customerMapping被填充 -
_context.Order.Include(y => y.customerMapping).FirstOrDefault(x => x.orderID == 1);
Run Code Online (Sandbox Code Playgroud)
加载父级时,将始终包含自有类型。此功能是通过在模型中设置属性来实现的,您可以为任何导航手动设置该属性。
modelBuilder.Entity<Order>()
.Navigation(d => d.customerMapping)
.AutoInclude(true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2222 次 |
| 最近记录: |