简体   繁体   English

难于保存

[英]nhibernate difficulty in save

i have this the code and i have this problem 我有这个代码,我有这个问题

  • i am trying to save a new user to my web site : the query is succeded but without insertion 我正在尝试将新用户保存到我的网站:查询成功但没有插入
  • ie the table "User" and the table "UserAcl" still without any modification and it is clear that the query is executed File User.cs: 即表“ User”和表“ UserAcl”仍然没有任何修改,并且很明显查询是在文件User.cs中执行的:

File 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; }

           }

File Administration.cs 文件管理.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");

        }

First, you could use if (!String.IsNullOrEmpty(myString)) rather than if (myString==null) . 首先,可以使用if (!String.IsNullOrEmpty(myString))而不是if (myString==null) Also, you may want to use your sessions within a using block. 另外,您可能希望在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;

Now, about the query not inserting, it could be a mapping problem, or maybe you're supressing the exception elsewhere (like your static method User.OpenSession().SaveOrUpdate(u) ) 现在,关于查询未插入的问题,可能是映射问题,或者您正在其他地方抑制异常(例如您的静态方法User.OpenSession().SaveOrUpdate(u)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM