【发布时间】:2013-09-12 10:50:00
【问题描述】:
我已将一些存储过程导入到 EF。
只使用存储过程,不使用 LINQ to Entities。
这方面的最佳做法是什么?
我只是有一个使用Entities 字段来调用存储过程的类:
public class DataAccess : IDataAccess
{
private readonly MyEntities entities = new MyEntities();
public IEnumerable<MyObj> GetMyObJects()
{
return entities.GETMyObj().Select(x => new MyObj { Name = x.NAME }).ToList();
}
public IEnumerable<MyObj2> GetMyObJects2()
{
return entities.GETMyObj2().Select(x => new MyObj2 { Name = x.NAME }).ToList();
}
public IEnumerable<MyObj3> GetMyObJects3()
{
return entities.GETMyObj3().Select(x => new MyObj3 { Name = x.NAME }).ToList();
}
}
这是推荐的吗?我应该为每种方法使用新的Entities 吗?
如下:
public IEnumerable<MyObj3> GetMyObJects3()
{
using(MyEntities entities = new MyEntities())
{
return entities.GETMyObj3().Select(x => new MyObj3 { Name = x.NAME }).ToList();
}
}
【问题讨论】:
-
我认为最佳做法是将上下文包装在 using 中,以便尽快处理它,但根据 DataAccess 类的生命周期,差异可能可以忽略不计
标签: c# .net entity-framework stored-procedures