locked
An exception of type 'NHibernate.Exceptions.GenericADOException' RRS feed

  • Question

  • User-1922896715 posted

    I am trying to make a registration. Everything is fine until the entity save with NHibernate, when this exception occurs: 

    An exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll but was not handled in user code

    Additional information: could not insert: [DataAccess.Model.PujcovnaUser][SQL: INSERT INTO pujcovna_user (name, surname, login, password, address, role_id) VALUES (?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]

    In debug mode, when i click continue, this pops up: 

    Cannot insert the value NULL into column 'role_id', table 'Pujcovna_lyzi.dbo.pujcovna_user'; column does not allow nulls. INSERT fails.
    The statement has been terminated.

    However when i drag mouse on the entity, the role_id is correctly set and is not null.

    USER MAPPING

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="DataAccess"
                       namespace="DataAccess.Model">
    
      <class name="PujcovnaUser" table="pujcovna_user" lazy="false">
        <id name="Id" column="user_id">
          <generator class="native" />
        </id>
        <property name="Name" column="name" />
        <property name="Surname" column="surname" />
        <property name="Login" column="login" />
        <property name="Password" column="password" />
        <property name="Address" column="address" />
        <many-to-one name="Role" column="role_id" foreign-key="role_id" />
      </class>
    
    </hibernate-mapping>

    ROLE MAPPING

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="DataAccess"
                       namespace="DataAccess.Model">
    
      <class name="PujcovnaRole" table="pujcovna_role">
        <id name="Id" column="Id">
          <generator class="native" />
        </id>
        <property name="Identificator" column="identificator" />
        <property name="RoleDescription" column="role_description" />
      </class>
    
    </hibernate-mapping>

    USER CONTROLLER

     public class UserController : Controller
        {
            // GET: Admin/User
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult Create()
            {
    
                UserDao userDao = new UserDao();
                IList<PujcovnaUser> users = userDao.GetAll();
                ViewBag.Users = users;
                
    
                return View();
            }
    
            [HttpPost]
            public ActionResult Add(PujcovnaUser user)
            {
                if (ModelState.IsValid)
                {
    
    
                    UserDao userDao = new UserDao();
    
                    PujcovnaRoleDao roleDao = new PujcovnaRoleDao();
                    PujcovnaRole role = roleDao.GetById(2);
    
                    user.Role = role;
    
                    userDao.Create(user);
                    TempData["message-success"] = "Uživatel vytvořen";
    
                }
    
                else
    
                {
                    return View("Create", user);
                }
    
    
    
                return RedirectToAction("Index");
            }
        }

     

    Can you help me find the mistake please?

    Thursday, August 24, 2017 10:57 PM

All replies