Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 在ef中可以查询的模型中无法获取列表?_Asp.net Core_.net Core_Entity Framework Core - Fatal编程技术网

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
将不包括b
User
用户
实体仍将返回空值
_context.Products.Include(p => p.Users).AsQueryable();