开发者

how can i decrease memory usage by entity framework

开发者 https://www.devze.com 2023-02-23 13:40 出处:网络
i made one CMS on .NET4 And Entity Framework. this CMS in now begin used on server with shared memory.

i made one CMS on .NET4 And Entity Framework. this CMS in now begin used on server with shared memory.

application pool has limited memory so app pool reset when my application reach the memory limit . after trace my app i realiz开发者_Python百科e EF use most of memory . is there any way to decrease memory usage of EF or any tweaking?

my sample use of EF :

using BICT.Data;
//
public static List<Data.Log> GetList()
{
    using(CMSEntities cms = new CMSEntities())
    {
       return cms.Log.ToList<Data.Log>();
    }
}

am i use a wrong way to use EF?


Assuming CMSEntities is your dbContext, then you're using it the right way as it implements the IDisposable interface.

One thing I would suggest is returning an IQueryable instead of a List as then you gain the flexibility of pipelining Linq queries, and then actually accessing the database. (Lazy Loading).

If you want all of the items, you can call .ToList(), so that option is still available to you.


EDIT

For example, say I have a User table in my database, I create a wrapper class for accessing the data.

namespace Data.Repositories
{
    public class UserRepository : IDisposable
    {
        ColegioDBV2Entities db = new ColegioDBV2Entities();

        public IQueryable<User> FindAllUsers()
        {
            return db.Users;
        }    

        public void Dispose()
        {
            db.Dispose();
        }
    }
}

And when I want to access data I do so through this repository class:

using (UserRepository repo = new UserRepository())
{
    var result = repo.FindAllUsers().Where(u => u.Username == txtUsername.Text && u.Password == txtPassword.Text);                    
}

Once you leave that using block, your context is automatically closed and released.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号