【问题标题】:Large data object - am I worrying too much大数据对象——我是不是太担心了
【发布时间】:2012-06-12 18:59:28
【问题描述】:

我是第一次使用 EF,所以我不知道这种情况是正常的还是我有严重的性能问题。
我有以下情况:

下面是我的课程。项目是这里的主要对象。因此,当我从数据库中提取项目列表时,我会得到例如 1000 个项目。现在,每个项目都具有数据归档的所有属性。城市包含国家,国家包含城市列表,用户有创建项目的列表,每个项目的所有数据再次,城市,城市有国家,国家城市列表等...
也许我太担心了,我不知道这个对象是否应该包含所有这些数据,这是否会导致性能问题,或者我在这里做错了什么?

public abstract class Item
    {
        [Key]
        public int ItemId { get; set; }

        public int ItemTypeId { get; set; }
        public Guid UserId { get; set; }
        public DateTime CreatedOnDate { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int? MediaId { get; set; }
        public int CityId { get; set; }
        public virtual City City { get; set; }
        public virtual User User { get; set; }
        public virtual ICollection<ItemInBoard> ItemsInBoard { get; set; }
        public virtual ICollection<Like> Likes { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }        
    }
public class City
    {
        public int CityId { get; set; }
        public string Name { get; set; }
        public double Longitude { get; set; }
        public double Latitude { get; set; }
        public int CountryId { get; set; }
        public virtual Country Country { get; set; }
    }
public class Country
    {
        public int CountryId { get; set; }
        public string Name { get; set; }
        public string CountryCode { get; set; }
        public virtual ICollection<City> Cities { get; set; }
    }
public class User
    {
        public Guid UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool Gender { get; set; }
        public DateTime? BirthDay { get; set; }
        public string AboutMe { get; set; }
        public int? MediaId { get; set; }
        public int CityId { get; set; }
        public virtual City City { get; set; }
        public virtual ICollection<Item> Items { get; set; }
        public virtual ICollection<Board> Boards { get; set; }
        public virtual ICollection<Like> Likes { get; set; }
    }

【问题讨论】:

    标签: c# entity-framework entity-framework-4.1


    【解决方案1】:

    这取决于你。这是一个称为延迟加载的概念。您可以使用以下代码启用或禁用延迟加载:

      context.Configuration.LazyLoadingEnabled = false;
      context.Configuration.LazyLoadingEnabled = true;
    

    启用此选项后,将不会加载任何依赖实体。要强制加载依赖实体,您可以使用 Include lambada 表达式,如下所示:

       var test = context.Tests.Include("SomeOtherDependentEntity");
    

    希望我得到了你,这就是你的意思。

    【讨论】:

      【解决方案2】:

      我会说你所拥有的对于一般业务逻辑来说很好。

      当我必须以只读方式进行大量时间敏感处理时,我会使用这样的 SQL 命令来准确且仅准确地获取我想要的内容。

      public class myQueryClass
      {
      public string Property1 { get; set; }
      public string Property2 { get; set; }
      }
      
      var context = new MyDbContext();
      context.Database.SqlQuery<myQueryClass>("SELECT Property1 = acolumn, Property2 = acolumn2 FROM myTable WHERE something = somestate");
      

      【讨论】:

        猜你喜欢
        • 2012-02-15
        • 2019-09-07
        • 1970-01-01
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多