C# EF和MVC——合作的方法
我长期(大约5年)使用了以下方法: 在controller中创建一个初始化为xxxenties的大类,并使用DB为每个操作创建每个方法。例如:C# EF和MVC——合作的方法,c#,asp.net-mvc,entity-framework,async-await,C#,Asp.net Mvc,Entity Framework,Async Await,我长期(大约5年)使用了以下方法: 在controller中创建一个初始化为xxxenties的大类,并使用DB为每个操作创建每个方法。例如: public class DBRepository { private MyEntities _dbContext; public DBRepository() { _dbContext = new MyEntities(); } public NewsItem NewsItem(int ID)
public class DBRepository
{
private MyEntities _dbContext;
public DBRepository()
{
_dbContext = new MyEntities();
}
public NewsItem NewsItem(int ID)
{
var q = from i in _dbContext.News where i.ID == ID select new NewsItem() { ID = i.ID, FullText = i.FullText, Time = i.Time, Topic = i.Topic };
return q.FirstOrDefault();
}
public List<Screenshot> LastPublicScreenshots()
{
var q = from i in _dbContext.Screenshots where i.isPublic == true && i.ScreenshotStatus.Status == ScreenshotStatusKeys.LIVE orderby i.dateTimeServer descending select i;
return q.Take(5).ToList();
}
public void SetPublicScreenshot(string filename, bool val)
{
var screenshot = Get<Screenshot>(p => p.filename == filename);
if (screenshot != null)
{
screenshot.isPublic = val;
_dbContext.SaveChanges();
}
}
public void SomeMethod()
{
SomeEntity1 s1 = new SomeEntity1() { field1="fff", field2="aaa" };
_dbContext.SomeEntity1.Add(s1);
SomeEntity2 s2 = new SomeEntity2() { SE1 = s1 };
_dbContext.SomeEntity1.Add(s2);
_dbContext.SaveChanges();
}
可能是这样一种情况,即下一个代码将在SaveChanged完成之前执行,并且将向_dbContext中添加一个或多个实体,在某些操作之前不应保存这些实体。例如,一些代码:
DBRepository _rep = new DBRepository();
_rep.AddStatSimplePageAsync("A", "b", "c");
_rep.SomeMethod();
我担心会在下线后调用SaveChanged
_dbContext.SomeEntity1.Add(s1);
但之前
_dbContext.SomeEntity2.Add(s2);
(即这两个动作是原子操作)
我说得对吗?我的方法现在错了?应该采用哪种方法
注:据我所知,将是以下堆栈:
1. calling AddStatSimplePageAsync
2. start calling await _dbContext.SaveChangesAsync(); inside AddStatSimplePageAsync
3. start calling SomeMethod(), _dbContext.SaveChangesAsync() in AddStatSimplePageAsync is executing in another (child) thread.
4. complete _dbContext.SaveChangesAsync() in child thread. Main thread is executing something in SomeMethod()
好吧,这次我(想)你有问题了
首先,奇怪的是,您有两个单独的调用SaveChanges
方法。通常,您应该在所有操作结束时尝试使用它,然后将其处理掉
即使你认为是的,你的担心是对的,但这里需要澄清
async
或await
时,不要考虑线程,而要考虑任务简单地说,如果您不等待异步方法,那么后续操作可能会“损害”第一个方法的执行。要解决它,只需等待它。我不确定是否收到了你的问题。看起来您担心异步操作,如果在另一个请求出现时没有完成,可能会给您带来一些代码分配问题?您能详细说明一下吗?在主要问题中添加了详细信息这不是异步/等待操作的工作方式。Async/await不会转移到另一个需要
await
函数调用结果的函数。您当前的方法对我来说似乎很好,为了进一步阅读,请检查MSDN,特别是我在这里链接的部分-此外,您的示例编辑,您没有等待您的AdStatSimplePageAsync
方法,因此,它将正常(同步)运行。据我所知,方法AddStatSimplePageAsync在wait _dbContext.saveChangesSync()调用时,将控制权“返回”到我的主方法(将启动_rep.SomeMethod()),但尚未完成。请看我问题的结尾,我添加了一些内容
1. calling AddStatSimplePageAsync
2. start calling await _dbContext.SaveChangesAsync(); inside AddStatSimplePageAsync
3. start calling SomeMethod(), _dbContext.SaveChangesAsync() in AddStatSimplePageAsync is executing in another (child) thread.
4. complete _dbContext.SaveChangesAsync() in child thread. Main thread is executing something in SomeMethod()