Asp.net mvc 实体框架不返回相关对象
因此,我试图将两个表相互关联起来。我有一个产品表和一个图像表。我在产品对象中有一个图像集合,在图像对象/表中有一个产品ID,但是当它得到产品对象时,图像集合是空的 有趣的是,在调试时,如果我在product对象从中提取信息之前检查context元素,它会在context元素中加载图像,然后将其分配给产品。因此,只有当我手动检查上下文元素并搜索图像时,它才会起作用 产品Asp.net mvc 实体框架不返回相关对象,asp.net-mvc,entity-framework,asp.net-core,Asp.net Mvc,Entity Framework,Asp.net Core,因此,我试图将两个表相互关联起来。我有一个产品表和一个图像表。我在产品对象中有一个图像集合,在图像对象/表中有一个产品ID,但是当它得到产品对象时,图像集合是空的 有趣的是,在调试时,如果我在product对象从中提取信息之前检查context元素,它会在context元素中加载图像,然后将其分配给产品。因此,只有当我手动检查上下文元素并搜索图像时,它才会起作用 产品 public class Product { public int Id { get; set; }
public class Product
{
public int Id { get; set; }
public string FriendlyUrl { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public double? Price { get; set; }
public double? Weight { get; set; }
public int? Stock { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public class TheContext : DbContext
{
public TheContext(DbContextOptions<TheContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Image> Images { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasMany<Image>(s => s.Images);
}
}
DBContext
public class Product
{
public int Id { get; set; }
public string FriendlyUrl { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public double? Price { get; set; }
public double? Weight { get; set; }
public int? Stock { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public class TheContext : DbContext
{
public TheContext(DbContextOptions<TheContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Image> Images { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasMany<Image>(s => s.Images);
}
}
这是由于EF延迟加载特性造成的。好像已经关机了。 含义:您需要明确指定要加载的内容。如果它处于打开状态,则在首次访问该集合时将加载该集合 你可以在这里了解更多 或 或
您需要加载相关实体,在您的情况下,图像: 数据访问
public Product GetById(string id)
{
Product product = _context.Products.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
public Product GetById(string id)
{
Product product = _context.Products.include(p => p.Images)
.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
public Product GetById(string id)
{
using(var context = new TheContext())
{
product = context.Products.include(p => p.Images)
.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
}
在此处阅读有关加载相关实体的更多信息:
此外,您希望通过在需要的地方使用工厂或实例化上下文,尽快处理上下文,以避免内存泄漏
数据访问
public Product GetById(string id)
{
Product product = _context.Products.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
public Product GetById(string id)
{
Product product = _context.Products.include(p => p.Images)
.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
public Product GetById(string id)
{
using(var context = new TheContext())
{
product = context.Products.include(p => p.Images)
.FirstOrDefault(p => p.FriendlyUrl == id);
return product;
}
}
不要使字段变量中的上下文保持活动状态
在此处阅读有关生命周期的更多信息:
\u context.Products.Include(x=>x.Images).FirstOrDefault(…
尝试使用modelBuilder.Conventions.Remove();