Tuesday, 15 January 2013

c# - Duplicate class/entity mapping in Fluent NHibernate -



c# - Duplicate class/entity mapping <ClassName> in Fluent NHibernate -

edit: added hbm mapping @ end of post.

i have rather big class library, inheritance involved. i'm using fluent nhibernate map classes ms sql 2008 host in azure.

i'm sure mappings correct. utilize discriminator value differentiate classes on column (not on table). however, whenever seek generate mappings, error 1 of class/entity duplicate.

duplicate class/entity mapping folkelib.mmobash.bash

this happens here:

private static isessionfactory createsessionfactory() { homecoming fluently.configure() .database( mssqlconfiguration.mssql2008.dialect("nhibernate.dialect.mssql2008dialect") .connectionstring(c => c.fromconnectionstringwithkey("folkeconnstring")) .showsql()) .mappings(m => m.fluentmappings.addfromassemblyof<account>()) .exposeconfiguration(cfg => new schemaupdate(cfg).execute(false, true)) .diagnostics(diag => diag.enable().outputtoconsole()) .buildsessionfactory(); }

the class hierarchy this

message comment forummessage article image bash

bash inherits image, inherits article, , on. have different discriminator value. message mappings defined this:

public class messagemap : classmap<message> { messagemap() { id(x => x.id); discriminatesubclassesoncolumn("messagetype").sqltype("int32"); map(x => x.creationdate).index("articlecreationdate"); map(x => x.modificationdate); map(x => x.lastchildcreationdate); references(x => x.author); references(x => x.rootmessage).nullable(); ; references(x => x.parentmessage).nullable(); ; } }

(there more properties irrelevant think). note rootmessage , parentmessages of type "message." root cause?

bash mappings this:

public class bashmap : subclassmap<bash> { bashmap() { discriminatorvalue(5); map(x => x.game); map(x => x.language); references(x => x.approvedby); } }

i have spent hours on , have no thought why duplicate class/entity mapping error.

edit: troubleshooting update.

following suggestions below added bit of code:

public actionresult zogzog() { list<string> types = new list<string>(); foreach (var module in typeof(account).assembly.getmodules()) { foreach (var type in module.gettypes()) { if (typeof(imappingprovider).isassignablefrom(type)) { types.add(type.tostring()); } } } viewbag.types = types; homecoming view(); }

the output of view this:

folkelib.calendar.eventrolemap

folkelib.calendar.locationmap

folkelib.calendar.registrationmap

folkelib.calendar.rolemap

folkelib.domain.accountmap

folkelib.domain.accountbanmap

folkelib.domain.communitymap

folkelib.domain.contactentrymap

folkelib.domain.contacttypemap

folkelib.domain.forummap

folkelib.domain.readmessagemap

folkelib.domain.rssfeedmap

folkelib.domain.skinmap

folkelib.domain.groupmap

folkelib.domain.languagemap

folkelib.domain.menumap

folkelib.domain.menuitemmap

folkelib.domain.messagemap

folkelib.domain.pollmap

folkelib.domain.pollanswermap

folkelib.domain.pollvotemap

folkelib.domain.quotemap

folkelib.domain.messagereportmap

folkelib.game.charactermap

folkelib.domain.rsschannelmap

folkelib.domain.sitemap

folkelib.domain.siteapplicationmodulemap

folkelib.domain.statictextmap

folkelib.domain.tagmap

folkelib.domain.votemap

i don't seem have duplicates, none of children of message seem appear. i'm not sure if normal (they of type subclassmap, shown here of type classmap) or not asked in comments.

edit: message.hbm.xml file generated fluent. can see, classes (bash, article, ...) shown several time subclasses, different discriminator values!

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> <class xmlns="urn:nhibernate-mapping-2.2" name="folkelib.domain.message, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" table="`message`"> <id name="id" type="system.int32, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="id" /> <generator class="identity" /> </id> <discriminator type="string"> <column name="messagetype" sql-type="int32" /> </discriminator> <property name="creationdate" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="creationdate" index="articlecreationdate" /> </property> <property name="modificationdate" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="modificationdate" /> </property> <property name="lastchildcreationdate" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="lastchildcreationdate" /> </property> <property name="text" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="text" /> </property> <property name="locked" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="locked" /> </property> <property name="score" type="system.int32, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="score" /> </property> <property name="hidden" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="hidden" /> </property> <property name="deleted" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="deleted" /> </property> <property name="authorip" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="authorip" /> </property> <property name="publicationdate" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="publicationdate" /> </property> <many-to-one class="folkelib.domain.account, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="author"> <column name="author_id" /> </many-to-one> <many-to-one class="folkelib.domain.account, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="locker"> <column name="locker_id" not-null="false" /> </many-to-one> <many-to-one class="folkelib.domain.message, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="rootmessage"> <column name="rootmessage_id" not-null="false" /> </many-to-one> <many-to-one class="folkelib.domain.message, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="parentmessage"> <column name="parentmessage_id" not-null="false" /> </many-to-one> <many-to-one class="folkelib.domain.site, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="site"> <column name="site_id" not-null="true" /> </many-to-one> <many-to-one class="folkelib.domain.forum, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="forum"> <column name="forum_id" /> </many-to-one> <many-to-one class="folkelib.domain.account, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="deletedby"> <column name="deletedby_id" /> </many-to-one> <subclass name="folkelib.domain.comment, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="2" /> <subclass name="folkelib.domain.forummessage, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="3"> <property name="title" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="title" /> </property> <subclass name="folkelib.calendar.event, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="6"> <bag lazy="true" name="roleset" table="eventeventrole"> <key> <column name="event_id" /> </key> <many-to-many class="folkelib.calendar.eventrole, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null"> <column name="eventrole_id" /> </many-to-many> </bag> <property name="duration" type="system.timespan, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="duration" /> </property> <property name="recurringdays" type="system.int32, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="recurringdays" /> </property> <property name="firsttime" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="firsttime" /> </property> <property name="lasttime" type="system.datetime, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="lasttime" /> </property> <many-to-one class="folkelib.calendar.location, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="location"> <column name="location_id" /> </many-to-one> </subclass> <subclass name="folkelib.domain.article, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="1"> <subclass name="folkelib.domain.image, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="1"> <property name="name" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="name" /> </property> <property name="extension" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="extension" /> </property> <property name="public" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="public" /> </property> <property name="foradults" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="foradults" /> </property> <subclass name="folkelib.mmobash.bash, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="5"> <property name="game" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="game" /> </property> <property name="language" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="language" /> </property> <many-to-one class="folkelib.domain.account, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="approvedby"> <column name="approvedby_id" /> </many-to-one> </subclass> </subclass> </subclass> <subclass name="folkelib.domain.article, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="4"> <bag name="tagset" table="tagtoarticle"> <key> <column name="article_id" /> </key> <many-to-many class="folkelib.domain.tag, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null"> <column name="tag_id" /> </many-to-many> </bag> <bag name="imageset" table="imagetoarticle"> <key> <column name="article_id" /> </key> <many-to-many class="folkelib.domain.image, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null"> <column name="image_id" /> </many-to-many> </bag> <property name="extratext" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="extratext" /> </property> <subclass name="folkelib.domain.image, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="1"> <property name="name" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="name" /> </property> <property name="extension" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="extension" /> </property> <property name="public" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="public" /> </property> <property name="foradults" type="system.boolean, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="foradults" /> </property> <subclass name="folkelib.mmobash.bash, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" discriminator-value="5"> <property name="game" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="game" /> </property> <property name="language" type="system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"> <column name="language" /> </property> <many-to-one class="folkelib.domain.account, folkelib, version=1.0.0.0, culture=neutral, publickeytoken=null" name="approvedby"> <column name="approvedby_id" /> </many-to-one> </subclass> </subclass> </subclass> </subclass> </class> </hibernate-mapping>

somebody found reason. there error in mappings of class:

this issue:

public class articlelistmap : subclassmap<article> { articlelistmap() { discriminatorvalue(1); } } public class articlemap : subclassmap<article> { articlemap() { discriminatorvalue(4); map(x => x.extratext); hasmanytomany(x => x.tagset); hasmanytomany(x => x.imageset); } }

instead of inheriting articlelistmap subclassmap<articlelist> inheriting subclassmap<article> (which typing mistake). since have different discriminators inherit same class there was, indeed, duplicate mappings "propagated" children classes.

c# asp.net-mvc-3 fluent-nhibernate

No comments:

Post a Comment