NHibernate frequently asked questions and how to solve them
Have learned nhibernate, but since the work to now almost a year has never been used, recently to consolidate but found to forget a lot, a "in expected: <end-of-text> (possibly an invalid or Unmapped class name is used in the query). "The error was checked long time finally found." This article is a common mistake I reproduced in nhibernate ...
- the issue may be caused by the version number in the Hbm.xmlnhibernate file. The 2.2 here represents the NHibernate version number and must match the product version number of the nhibernate you installed. Otherwise, for example, if it is urn:nhibernate-mapping-2.0, the following error will occur: Could not find Schema information for the element ' urn:nhibernate-mapping-2.0:hibernate-mapping '. Or: "The NHibernate.Cfg.Environment type initializer throws an exception."
- If the attribute is not added to the virtual keyword in the control class, it may be reported
NHibernate.InvalidProxyTypeException:The following types May is used as proxies:
Model.FriendLink:method Set_description should be virtual
Model.Type:method get_typename should be virtual .....
One solution is to add the virtual keyword to the attribute, and the other workaround is to include default-lazy= "false" in the mapping file.
- In expected: <end-of-text> (possibly an invalid or unmapped class name is used in the query).
The mapping file does not have the attribute set to "Embedded Resource" or the case of table name when the method is adjusted
" Could not find the dialect in the configuration "exception
- Exception Description:
NHibernate.MappingException:Could not compile the mapping document:Model.FriendLink.hbm.xml---> System.InvalidOperationException:Could not find the dialect in the configuration
NHibernate.Dialect.Dialect . Getdialect (IDictionary ' 2 props)
in NHibernate.Cfg.Configuration.AddValidatedDocument (namedxmldocument doc
Workaround:
configuration file xmlns= "urn:nhibernate-configuration-2.2" never forget to make sure you don't forget xmlns= "urn: nhibernate-configuration-2.2 "can solve this bug.
< Session-factory>
<property name= "Connection.provider" > Nhibernate.connection.driverconnectionprovider</property>
<property name= "Use_outer_join" >true </property>
<property name= "Connection.driver_class" >nhibernate.driver.sqlclientdriver</ Property>,
<property name= "Show_sql" >TRUE</PROPERTY>
<property name= "dialect" > Nhibernate.dialect.mssql2005dialect</property>
<property name= "connection.connection_string" > Server=.\sqlexpress;initial Catalog=sblog; User Id=sa; Password=1;</property>
<mapping assembly= "XMGL. Model "/>
</session-factory>
- Exception description for "Failed to load file or assembly Castle.dynamicproxy2":
System.TypeInitializationException: The type initializer for "NHibernate.Proxy.Poco.Castle.CastleProxyFactory" throws an exception. ---> System.IO.FileNotFoundException: Failed to load file or assembly "Castle.dynamicproxy2, version=2.0.3.0, Culture=neutral, PUBLICKEYTOKEN=407DD0808D44FBDC "or one of its dependencies. The system cannot find the file specified.
File name: "Castle.dynamicproxy2, version=2.0.3.0, culture=neutral, PUBLICKEYTOKEN=407DD0808D44FBDC"
In NHibernate.Proxy.Poco.Castle.CastleProxyFactory. Cctor ()
Workaround:
The method of the exception is relatively simple, and it is possible to add references in the assembly.
- The use of timestamp.
Feeling nhibernate is not good for timestamp support, I defined a timestamp type column in SQL Server 2005, mapped to a datetime type in the mapping file, and then reported:
NHibernate.ADOException:Could not cast the value in field upsize2_0_ of type byte[] to the type timestamptype. plea SE Check to make sure, the mapping is correct and that your dataprovider supports this Data Type. ---> System.InvalidCastException: The object of type ' system.byte[' cannot be cast to type ' system.iconvertible '.
in System.Convert.ToDateTime (Object value)
in NHibernate.Type.TimestampType.Get ( IDataReader RS, Int32 index)
in NHibernate.Type.NullableType.NullSafeGet (IDataReader RS, String name) The
has not been well-equipped for half a day. You can only change the database column to a datetime type, and then configure the mapping file in the following format:
<id name= "ContentId" type= "String" unsaved-value= "null" > <column name= "ContentId" length= "$" sql-type= "nvarchar" not-null= "true" unique= "true" index= "aaaaacontent_pk"/> <generator class= "Assigned "/> </id> <timestamp name= "upsizets" column= "Upsize_ts"/>
- Row is updated or deleted by another transaction (or Unsaved-value mapping was incorrect)
One cause of this error is a database lock, which is nhibernate by default. Information about optimistic locking in nhibernate and how to resolve errors can be seen there.
- Identity_insert is off
SET IDENTITY_INSERT table name on in Query Analyzer
- The data item was not found in the container. The container must implement Idataitemcontainer or have a property named DataItem.
<id name= "D_name" column= "D_name" type= "System.String" >
<generator class= "Assigned"/>
</id> primary key field must be a field that exists for the current tableThe element ' class ' in namespace ' urn:nhibernate-mapping-2.2 ' have invalid child element ' property ' in namespace ' urn:n hibernate-mapping-2.2 '. List of possible elements expected: ' Urn:nhibernate-mapping-2.2:meta urn:nhibernate-mapping-2.2:jcs-cache urn: Nhibernate-mapping-2.2:cache urn:nhibernate-mapping-2.2:id Urn:nhibernate-mapping-2.2:composite-id '.
NHibernate frequently asked questions and how to solve them