实体框架延迟加载

发布于 2024-12-05 07:46:51 字数 2034 浏览 3 评论 0原文

我有两个实体:

public class Product
    {
        [HiddenInput(DisplayValue=false)]
        public int ProductID { get; set; }

        [Required(ErrorMessage="Please enter a product name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required]
        [Range(0.01,double.MaxValue,ErrorMessage="Please enter positive price")]
        public decimal Price { get; set; }

        public Category Category { get; set; }

        [HiddenInput(DisplayValue= false)]
        public string ImageFileName { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }

    }

public class Category
    {
        [HiddenInput(DisplayValue=false)]
        public int CategoryID { get; set; }

        [Required(ErrorMessage="Please enter a category name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }


        public ICollection<Product> Products { get; set; }

        [HiddenInput(DisplayValue= false)]
        public string ImageFileName { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }
    }

我尝试以这种方式获取产品时,

Product product = repository.Products.FirstOrDefault(p => p.ProductID == id);

类别字段为空。

没有product.Category.Load() 和repository.Products.Include("Category")... 方法。

context.Configuration.LazyLoadingEnabled = false;

不影响。 上下文是下一个类的对象

 public class EFDbContext:DbContext 
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }
    }

我应该如何加载所需的字段?

谢谢

I have two entities:

public class Product
    {
        [HiddenInput(DisplayValue=false)]
        public int ProductID { get; set; }

        [Required(ErrorMessage="Please enter a product name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required]
        [Range(0.01,double.MaxValue,ErrorMessage="Please enter positive price")]
        public decimal Price { get; set; }

        public Category Category { get; set; }

        [HiddenInput(DisplayValue= false)]
        public string ImageFileName { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }

    }

and

public class Category
    {
        [HiddenInput(DisplayValue=false)]
        public int CategoryID { get; set; }

        [Required(ErrorMessage="Please enter a category name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }


        public ICollection<Product> Products { get; set; }

        [HiddenInput(DisplayValue= false)]
        public string ImageFileName { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }
    }

When I'm trying to get product this way

Product product = repository.Products.FirstOrDefault(p => p.ProductID == id);

Category field is null.

There is no product.Category.Load() and repository.Products.Include("Category")... methods.

context.Configuration.LazyLoadingEnabled = false;

does not affect.
Context is an object of next class

 public class EFDbContext:DbContext 
    {
        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }
    }

How I should load needed field?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城半夏 2024-12-12 07:46:51

也许您的映射定义有问题。您可以在类别表上执行 GetAll 操作吗?您的 Product.Category 属性是否知道类别表要使用哪个外键?

文章 在 EF 4.1 中使用 DbContext 第 6 部分:加载相关实体 解释了以急切或惰性模式加载实体的不同方法。

当您使用context.Configuration.LazyLoadingEnabled = false时,您正在定义加载实体的全局方式,但如果您愿意,您可以具体说明应如何加载每个属性通过这样做:

  • 延迟加载示例:public virtual Category Category { get;放;
  • 急切加载示例:public Category Category { get;放;因此

,如果它对您不起作用,我会检查您的映射定义,然后检查发送到您的数据库的 sql 查询。您将准确地看到 sql 查询是否包含您的类别详细信息。

我知道有 2 个选项可以查看生成的 sql。您可以监控数据库或使用 Mvc Mini Profiler。如果您使用 DevArt dotConnect,那么您就有一个专门用于此目的的监控工具。

Maybe you have a problem with your mapping definition. Are you able to do a GetAll on your Category table? Does your Product.Category property knows which Foreign key to use for the Category table?

The article Using DbContext in EF 4.1 Part 6: Loading Related Entities explain different ways to load your entities in eager or lazy mode.

When you use context.Configuration.LazyLoadingEnabled = false, you are defining the global way to load the entities but if you want, you can tell specificly how each properties should be loaded by doing this:

  • Example for lazy loading: public virtual Category Category { get; set; }
  • Example for eager loading: public Category Category { get; set; }

So if it doesnt work for you, i would check your mapping definition and then check the sql query that is sent to your database. You will see exactly if the sql query include your Category details or not.

I know 2 options to see the sql that is generated. You can monitor your database or use Mvc Mini Profiler. If you are using DevArt dotConnect, you have a monitoring tool just for that.

坚持沉默 2024-12-12 07:46:51

我刚刚尝试以这种方式获取 Product 对象:

Product product = (
   from p in repository.Products.Include("Category") 
   where p.ProductID == id 
   select p
).SingleOrDefault(); 

并且类别也已加载。这是可能的解决方案。但有趣的是为什么当我第一次使用时类别为空

I have just tried to fetch Product object such way:

Product product = (
   from p in repository.Products.Include("Category") 
   where p.ProductID == id 
   select p
).SingleOrDefault(); 

and category was loaded too. This is possible solution. But it is interesting why category is null when I'm using first

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文