NHibernate 如何知道要保存到哪个 table?

How does NHibernate know which table to save to?

我做了一些研究,因为我以前从未使用过 nhibernate,我发现要将实体保存到数据库,我应该使用 "SaveOrUpdate()" 所以我有以下内容:

Object myObject = new Object { someProperty="something" };
using (ISession session = NHibernateSessionFactoryManager.Factory.OpenSession())
{
    session.SaveOrUpdate(myObject);
}

我是否必须指定 table 我要保存到的某个地方,或者 NHibernate 是否从对象类型中找出它?

如果你查看 ISession 界面,你会发现只有两个 SaveOrUpdate 方法:

void SaveOrUpdate(object obj);

void SaveOrUpdate(string entityName, object obj);

您使用的是第一个,它使用您定义的映射将 obj.GetType() 映射到 table。

还有另一个使用 entityName 的重载,如果您使用

Persistent entities don't necessarily have to be represented as POCO classes at runtime. NHibernate also supports dynamic models (using Dictionaries of Dictionarys at runtime) . With this approach, you don't write persistent classes, only mapping files.

在这些映射文件中,您必须定义实体名称。