CSharp - 在 session.update() 和 session.delete( ) 之后,NHibernate: 数据库值未更改

  显示原文与译文双语对照的内容
98 1

Session.queryover() 和 session.save() 工作正常。 从数据库中获取数据或者将数据保存到数据库的所有逻辑都是通过和 session.delete() 对数据库进行工作,。 没有引发exeption使用 session.SaveOrUpdate 没有解决这里问题。 这是我的地图


[Serializable]
public class Requirement
{
 public virtual int Id { get; set; }
 public virtual int CreditRequired { get; set; }
 public virtual string Name { get; set; }
 public virtual IList<CourseRequirement> CourseRequirements
 {
 get
 {
 return new RequirementBC().getCourseRequirement(Id);
 }
 }


 public Requirement()
 { }

 public Requirement(DataRow row)
 {
 Id = int.Parse(row["Id"].ToString());
 CreditRequired = int.Parse(row["CreditRequired"].ToString());
 Name = row["Name"].ToString();
 }
}

public class RequirementMap : ClassMapping<Requirement>
{
 public RequirementMap()
 {
 Table("Requirements");
 Id<int>(x => x.Id, m => { m.Column("Id"); m.Generator(Generators.Native); });
 Property<int>(x => x.CreditRequired, m => { m.Column("CreditRequired");});
 Property<string>(x => x.Name, m => { m.Column("Name"); });
 }
}

这是我的逻辑


[Serializable]
public class RequirementBC 
{
 ISession session = NHibernateHelper.GetCurrentSession();
 public void UpdateRequirement(int reqId, string newName, int creditsRequired)
 {
 session.BeginTransaction();

 var req = session.QueryOver<Requirement>().Where(x => x.Id == reqId).SingleOrDefault<Requirement>();
 var old = session.QueryOver<Requirement>().Where(x => x.Name == newName && x.Id!= reqId).SingleOrDefault<Requirement>();
 if (old!= null)
 throw new Exception("Requirement with that name already exists");
 req.Name = newName;
 req.CreditRequired = creditsRequired;
 session.Update(req);
 session.Flush();
 session.Transaction.Commit(); 
 }
}

获取当前会话的逻辑


public static ISession GetCurrentSession()
{
 HttpContext context = HttpContext.Current;
 ISession currentSession = context.Items[CURRENT_NHIBERNATE_SESSION_KEY] as ISession;

 if (currentSession == null)
 {
 currentSession = sessionFactory.OpenSession();
 context.Items[CURRENT_NHIBERNATE_SESSION_KEY] = currentSession;
 }
 if (currentSession.Connection.State == System.Data.ConnectionState.Closed)
 {
 currentSession = sessionFactory.OpenSession();

 }
 if (!currentSession.IsConnected)
 {
 currentSession = sessionFactory.OpenSession();

 }
 if (!currentSession.IsOpen)
 {
 currentSession = sessionFactory.OpenSession();

 }
 if (currentSession.IsDirty())
 {
 currentSession.Clear();

 }

 return currentSession;
}

从我在这个论坛和它的他人的搜索中,那些遇到了一个异常或者另一个例外。 将感激任何帮助。

时间: 原作者:

52 5

你不需要刷新会话,如果你正在使用事务。 我还不确定你使用的事务语法。 如果我这样做,它将如下所示:


public void UpdateRequirement(int reqId, string newName, int creditsRequired)
{
 using(ISession session = NHibernateHelper.GetCurrentSession())
 {
 using(ITransaction transaction = session.BeginTransaction())
 {
 var req = session.QueryOver<Requirement>().Where(x => x.Id == reqId).SingleOrDefault<Requirement>();
 var old = session.QueryOver<Requirement>().Where(x => x.Name == newName && x.Id!= reqId).SingleOrDefault<Requirement>();

 if (old!= null)
 throw new Exception("Requirement with that name already exists");

 req.Name = newName;
 req.CreditRequired = creditsRequired;

 transaction.Commit(); 
 }
 }
}

原作者:
...