简体   繁体   中英

NHibernate ignore unknown entities

NHibernate ignore unknown entities

Hi, im struggling with the following situation:

I've got a single NHibernate mapping file for every database (schema) with multiple defined tables. For example:

NHibernate mapping file: A

<!-- Table ServiceModules -->
<class name="ServiceModules" table="SERVICE_MODULES" lazy="true" >
<id name="Id" column="ID">
  <generator class="native">
    <param name="sequence">SEQ_SERVICE_MODULES</param>
  </generator>
</id>
<property name="Name">
  <column name="NAME" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="Version">
  <column name="VERSION" sql-type="VARCHAR2" not-null="false" />
</property>
<property name="Description">
  <column name="DESCRIPTION" sql-type="VARCHAR2" not-null="false" />
</property>
</class>

<!-- Table Service -->
<class name="Service" table="SERVICE" lazy="true" >
<id name="Id" column="ID">
  <generator class="native">
    <param name="sequence">SEQ_SERVICE</param>
  </generator>
</id>
<property name="Name">
  <column name="NAME" sql-type="VARCHAR2" not-null="false" />
</property>
</class>
.
.
.

Next to this NHibernate mapping file i've got a library project which hosts all referenced classes in this database. For example:

NHibernate class project: B

public class ServiceModules
{
    public virtual decimal Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Version { get; set; }
    public virtual string Description { get; set; }
}

public class Service
{
    public virtual decimal Id { get; set; }
    public virtual string Name { get; set; }
}

.
.
.

I'm referencing the class project as well as the NHibernate mapping file in multiple projects. As soon as i create a new table in the database i also have to update the NHibernate mapping file A as well as the class project B in order to reflect the latest changes.

Now, if i do not update every existing (and running) solution which depends on the NHibernate mapping file A with the updated class library B , then i get the typical "NHibernate.MappingException: Unknown entity class" exception.

Is there a way to tell NHibernate to only load the classes which actually exists?

I am quite confident the answer is no. I do not think there is such a feature in NHibernate, or any convenient extension point for changing this behavior.

Personally, I avoid this trouble by embedding mappings in assembly as an embedded resource, like in my answer here . I see mappings as a part of the assembly, they should not change without recompiling the assembly.

I do not see mappings as configuration files: changing them usually involves code changes. In such case, re-building is required. So having them as embedded resources is really not an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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