首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >大型数据对象-我是否担心得太多了?

大型数据对象-我是否担心得太多了?
EN

Stack Overflow用户
提问于 2012-06-13 02:59:28
回答 2查看 91关注 0票数 0

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

我有以下情况:

下面是我的课程。Item是这里的主要对象。因此,当我从数据库中拉出一个条目列表时,我得到了例如1000个条目。现在,这项中的每一项都有包含数据的所有属性。城市包含国家,国家包含城市列表,用户有创建的项目列表,每个项目的所有数据再次,城市,城市有国家,国家的城市列表等。

也许我太担心了,我不知道这个对象是否应该包含所有这些数据,这是否会造成性能问题,或者我在这里做错了什么?

代码语言:javascript
复制
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; }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-13 03:06:44

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

代码语言:javascript
复制
  context.Configuration.LazyLoadingEnabled = false;
  context.Configuration.LazyLoadingEnabled = true;

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

代码语言:javascript
复制
   var test = context.Tests.Include("SomeOtherDependentEntity");

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

票数 3
EN

Stack Overflow用户

发布于 2012-06-13 03:16:52

我要说的是,对于一般的业务逻辑来说,您所拥有的是很好的。

当我必须以只读的方式进行大量对时间敏感的处理时,我会使用这样的SQL命令来获得我想要的东西。

代码语言:javascript
复制
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");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11003043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档