Asp.net core 在ef中可以查询的模型中无法获取列表?
我已将用户设置为产品的相对用户:Asp.net core 在ef中可以查询的模型中无法获取列表?,asp.net-core,.net-core,entity-framework-core,Asp.net Core,.net Core,Entity Framework Core,我已将用户设置为产品的相对用户: public class Product { public int Id{get;set;} public int UserId{get;set;} public Users User{get;set;} } 当我使用entityframework获取产品列表时 用户返回空值,为什么? User表中有一个值,UserId就在Product表中 b.HasOne("User").WithMany().HasForeignKey("Use
public class Product
{
public int Id{get;set;}
public int UserId{get;set;}
public Users User{get;set;}
}
当我使用entityframework
获取产品列表时
用户返回空值,为什么?
User
表中有一个值,UserId
就在Product
表中
b.HasOne("User").WithMany().HasForeignKey("UserID");
列表
中的项
具有User=null
,您需要包含要查找的实体。例如,假设我有以下上下文
AppDbContext.cs
var list = _context.Products.AsQueryable();
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Notification> Notifications { get; set; }
public DbSet<Offer> Offers { get; set; }
}
如果要使用通知
中的报价
实体,需要使用以下语句:
public class Notification
{
public int Id { get; set; }
public string Title { get; set; }
public int? OfferId { get; set; }
public virtual Offer Offer { get; set; }
}
在你的情况下:
context.Notifications.Include(n=> n.Offers).ToList();
// Your code goes here
您必须明确要求将用户包括在返回的列表中
var list = _context.Products.Include(p=> p.User).AsQueryable();
请添加您试图获取用户的代码with@MohamadMousheimish谢谢。我已经添加了。你能给你的上下文添加一个快照吗?我认为这是一个数据库集,对吗?FindAsync呢?var list=_context.Products.FindAsync(id)
FindAsync
将不包括bUser
。用户
实体仍将返回空值
_context.Products.Include(p => p.Users).AsQueryable();