Ian Herbert
Ian Herbert

Reputation: 1019

NHibernate - DuplicateMappingException duplicate import: view refers to both

I am getting this exception:

duplicate import: View refers to both TrackingPrototype.Models.vTSPrecedenceExclude, TrackingPrototype, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null and TrackingPrototype.Models.vDMPrecedenceExclude, TrackingPrototype, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null (try using auto-import="false")

I have 2 views, vDMPrecedenceExclude and vTSPrecedenceExclude.

2 mapping files:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TrackingPrototype"
                   namespace="TrackingPrototype.Models">

  <class name="vDMPrecedenceExclude" table="vDMPrecedenceExclude" entity-name="View">
    <id name="id" type="Int32" column="id" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <property name="EngagementID" />
    <property name="Issued" />
    <property name="ReturnedByPresenter" />
    <property name="SentToProducer" />
    <property name="ReturnedByProducer" />
    <property name="Executed" />
    <property name="ExcludedBitSet" />
    <property name="Change" />
  </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="TrackingPrototype"
                   namespace="TrackingPrototype.Models">

  <class name="vTSPrecedenceExclude" table="vTSPrecedenceExclude" entity-name="View">
    <id name="id" type="Int32" column="id" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <property name="EngagementID" />
    <property name="Received" />
    <property name="Drafted" />
    <property name="SentToProducer" />
    <property name="SentToPresenter" />
    <property name="ExcludedBitSet" />
  </class>
</hibernate-mapping>

Which are loaded like this:

configuration.AddFile(HttpContext.Current.Server.MapPath(@"Mappings\vDMPrecedenceExclude.hbm.xml"));
configuration.AddFile(HttpContext.Current.Server.MapPath(@"Mappings\vTSPrecedenceExclude.hbm.xml"));

The exception is referring to the second line.

and models defined as:

namespace TrackingPrototype.Models
{
    public class DealMemoTracking : Tracking
    {
        public virtual DateTime? Issued { get; set; }
        public virtual DateTime? ReturnedByPresenter { get; set; }
        public virtual DateTime? SentToProducer { get; set; }
        public virtual DateTime? ReturnedByProducer  { get; set; }
        public virtual DateTime? Executed { get; set; }
        public virtual int ExcludedBitSet { get; set; }
    }
}

namespace TrackingPrototype.Models
{
    public class TicketScalesTracking : Tracking
    {
        public virtual DateTime? Received { get; set; }
        public virtual DateTime? Drafted { get; set; }
        public virtual DateTime? SentToProducer { get; set; }
        public virtual DateTime? SentToPresenter { get; set; }
        public virtual int ExcludedBitSet { get; set; }
    }
}

namespace TrackingPrototype.Models
{
    public class vDMPrecedenceExclude : DealMemoTracking
    {
    }
}

namespace TrackingPrototype.Models
{
    public class vTSPrecedenceExclude : TicketScalesTracking
    {
    }
}

I am not fully understanding this error, any ideas why I am getting this exception?

Thank you.

Upvotes: 0

Views: 1848

Answers (1)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

As we can see in documentation

4.4. Dynamic models

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.

By default, NHibernate works in normal POCO mode. You may set a default entity representation mode for a particular ISessionFactory using the default_entity_mode configuration option

The following examples demonstrates the representation using Maps (Dictionary). First, in the mapping file, an entity-name has to be declared instead of (or in addition to) a class name:

<class entity-name="Customer">

So, that's it, if we do have a POCO class we should use the name attribute. Not the entity-name. The change which should make it:

// instead of this
// <class name="vDMPrecedenceExclude" table="vDMPrecedenceExclude" entity-name="View">
// use this
<class name="vDMPrecedenceExclude" table="vDMPrecedenceExclude">

Please check also this:

Upvotes: 1

Related Questions