【问题标题】:nhibernate difficulty in save休眠的保存难度
【发布时间】:2023-03-31 09:52:01
【问题描述】:

我有这个代码,我有这个问题

  • 我正在尝试将新用户保存到我的网站:查询成功但没有插入
  • 即“User”表和“UserAcl”表仍然没有任何修改,很明显查询已执行 文件用户.cs:

文件 Compte.cs

public bool SaveUser(string identification, string acc, string mot, string notify, string nom, string phone, string mail) {
               try
               {
                   if (identification == null || acc == null || nom == null || mail == null  || mot == null) return false;
                   ITransaction transaction = User.OpenSession().BeginTransaction();
                   User u = new User() { Account = acc, Identification = identification, ContactEmail = mail, ContactName = nom, ContactPhone = phone, NotifyEmail = notify, Password = mot };
                   User.OpenSession().SaveOrUpdate(u);
                   transaction.Commit();
                  ITransaction transaction2 = User.OpenSession().BeginTransaction();
                   Useracl ua = new Useracl { Account = acc, UserID = identification, AccessLevel = 1, AclID = (Useracl.GetUseracl().Count + 1).ToString() };
                   Useracl.OpenSession().SaveOrUpdate(ua);
                   transaction2.Commit();
                   return true;
               }
               catch { return false; }

           }

文件管理.cs

  public ActionResult Index()
       {

           ViewBag.Title = c.GetUserID().Count.ToString();
            return View();
        }
        public ActionResult BeforeRegister()
        {


            return View();
        }
        public ActionResult AfterRegister(string Pseudo, string Phone, string Email, string Password, string Notify)
        {

            bool a = c.SaveUser((c.GetPassword().Count + 1).ToString(), (c.GetPassword().Count + 1).ToString(), Password, Notify, Pseudo, Phone, Email);
            if (a)
            {
                return RedirectToAction("Index", "Administration");
            }
            else

                return RedirectToAction("BeforeRegister", "Administration");

        }

【问题讨论】:

  • 看起来您在每次调用 User.OpenSession() 时都打开了多个会话,因此您在一个会话上启动事务,并将用户保存在另一个会话中,然后为第一次会议,第二次会议从不做任何事情,然后在下一次重复这个。下面的答案给出了一个更好的方法。

标签: c# asp.net .net nhibernate razor


【解决方案1】:

首先,您可以使用if (!String.IsNullOrEmpty(myString)) 而不是if (myString==null)。 此外,您可能希望在 using 块内使用您的会话。

bool ret= new bool();
if ((!String.IsNullOrEmpty(foo1)) && (!String.IsNullOrEmpty(foo2)))
{
//ConnectionDB is a public class, with a static method ISessionFactory SessionFactory(), and the method OpenSession() returns an ISession 
 using (NHibernate.ISession nhSession = ConnectionDB.SessionFactory.OpenSession())
 {
 try
 {
  User u = new User();
  //set your User
  nhSession.Transaction.Begin();
  nhSession.Save(u);
  nhSession.Transaction.Commit();
  nhSession.Close();

  ret = true;
 }
 catch (Exception ex)
 {
 ret = false;
 }
}
return ret;

现在,关于未插入的查询,可能是映射问题,或者您在其他地方抑制了异常(例如您的静态方法 User.OpenSession().SaveOrUpdate(u)

【讨论】:

    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 2012-09-09
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 2013-11-28
    相关资源
    最近更新 更多