NHibernate中的映射错误 [英] Mapping Error in NHibernate
问题描述
我正在尝试使用NHibernate连接到Northwind数据库.但是由于某种原因,我无法加载实体类型.
I am trying to use NHibernate to connect to a Northwind database. But for some reason, I am not able to load the Entity Type.
这是我的Entity类
This is my Entity class
public class Product
{
public virtual Int32 ProductId { get; set; }
public virtual String Desc { get; set; }
}
这是我的地图
<class name="Product" table="Products">
<id name="ProductId" column="ProductId" type="Int32">
<generator class="identity"></generator>
</id>
<property name="Desc" column="ProductName" type="String" length="60">
</property>
</class>
我收到以下错误消息
无法加载实体:[OracleLinq.Product#12] [SQL:选择product0_.ProductId作为ProductId0_0_,product0_.ProductName作为ProductN2_0_0_ FROM产品product0_ WHERE product0_.ProductId =?]
could not load an entity: [OracleLinq.Product#12][SQL: SELECT product0_.ProductId as ProductId0_0_, product0_.ProductName as ProductN2_0_0_ FROM Products product0_ WHERE product0_.ProductId=?]
这是堆栈跟踪
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
at NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
at NHibernate.Impl.SessionImpl.ImmediateLoad(String entityName, Object id)
at NHibernate.Proxy.AbstractLazyInitializer.Initialize()
at NHibernate.Proxy.AbstractLazyInitializer.GetImplementation()
at NHibernate.Proxy.Poco.Castle.CastleLazyInitializer.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at ProductProxy4c67cf5bf6e640ab82d8c21a90e2a62b.set_Desc(String value)
at OracleLinq.Form1.Form1_Load(Object sender, EventArgs e)
我做错什么了吗?
推荐答案
第二次尝试:
这是您的注释中的配置,只是为了提高可读性:
Here is you configuration from your comments, just for readability:
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Product).Assembly);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
IDbConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;");
ISession session = sessionFactory.OpenSession(conn);
Product product = (Product)session.Load(typeof(Product), 12);
product.Desc = "";
很明显,NHibernate具有映射文件,它无法生成查询.
It's clear that NHibernate has the mapping file, it could not have generated the query.
可能是那个
- 数据库不存在:您已经检查了此内容.
- 表不存在:打开一个sql控制台(使用相同的连接字符串),然后将错误消息中的sql复制粘贴到该表中.能行吗?
- 连接未打开:请参见bollow
- the database is not there : you already checked this.
- the table is not there : open a sql console (using the same connect string) and copy-paste the sql from the error message into it. Does it work?
- the connection is not opened : see bollow
我认为您需要自己打开连接.更好的是,让NHibernate创建和管理连接.
I think that you need to open the connection yourself. Even better you let NHibernate create and manage the connections.
尝试一下:
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Product).Assembly);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
Product product = (Product)session.Load(typeof(Product), 12);
product.Desc = "";
连接字符串转到nhibernate.cfg.xml
the connectstring goes to the nhibernate.cfg.xml
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
<property name="connection.connection_string">
Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;
</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
这篇关于NHibernate中的映射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!