/ *更新* /
我认为会话和延迟加载存在一些问题。进一步的调查显示,我创建了重复的orderitems(否则在我的代码库中)。我可以关闭这个问题吗?
/ *原始问题* /
我有以下行动。
/运动/ MatrixDisplay / 33
这使得jquery ajax全部为
/ Campaign / MatrixDisplay / UpdateProduct(json)
UpdateProduct对数据库进行了一些更新。如果我检查数据库,他们会被保存。但是,如果我刷新主页“/ Campaign / MatrixDisplay / 33”,则返回先前的值。
这是我的json动作。
[Authorize]
[HttpPost]
public JsonResult UpdateProduct(string changedProducts, int productId, string previousProductTotal, UserSession userSession)
{
foreach (var orderItem in order.OrderItems)
{
if (orderItem.Id == orderItemId)
{
previousQuantity = orderItem.Quantity;
orderItem.Quantity = quantity;
_orderRepository.Update(order);
}
}
return this.Json(response, JsonRequestBehavior.AllowGet);
}
这是一个示例存储库
public class OrderRepository : Repository<Order>, IOrderRepository
{
private ISession _session;
public OrderRepository()
{
_session = NHibernateHelper.OpenSession();
}
public void Update(Model.Order order)
{
using (ITransaction transaction = _session.BeginTransaction())
{
_session.SaveOrUpdate(order);
transaction.Commit();
}
}
}
这是我的会话工厂
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
public const string SessionKey = "MySession";
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(UserProfile).Assembly);
configuration.SetProperty(NHibernate.Cfg.Environment.ConnectionStringName,
"xxxxxx");
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
var context = HttpContext.Current;
if (context != null && context.Items.Contains(SessionKey))
{
//Return already open ISession
return (ISession)context.Items[SessionKey];
}
else
{
//Create new ISession and store in HttpContext
var newSession = SessionFactory.OpenSession();
if (context != null)
context.Items[SessionKey] = newSession;
return newSession;
}
}
}