DuplicateMappingException with AutoMapping in RC1

626 views
Skip to first unread message

Scott Cowan

unread,
Aug 17, 2009, 6:00:11 PM8/17/09
to Fluent NHibernate
Summary:

I want to save two classes of the same name and different namespaces
with the Fluent NHibernate Automapper in RC1

Context

I'm writing having to import a lot of different objects to database
for testing. I'll eventually write mappers to a proper model.

I've been using code gen and Fluent NHibernate to take these DTOs and
dump them straight to db.

the exception does say to (try using auto-import="false")

Code

public class ClassConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
instance.Table(instance.EntityType.Namespace.Replace(".",
"_"));
}
}

namespace Sample.Models.Test1
{
public class Test
{
public virtual int Id { get; set; }
public virtual string Something { get; set; }
}
}

namespace Sample.Models.Test2
{
public class Test
{
public virtual int Id { get; set; }
public virtual string SomethingElse { get; set; }
}
}
And here's the actual app code

var model = AutoMap.AssemblyOf<Service1>()
.Where(t => t.Namespace.StartsWith("Sample.Models"))
.Conventions.AddFromAssemblyOf<Service1>();
var cfg = Fluently.Configure()
.Database(
MySQLConfiguration.Standard.ConnectionString(
c => c.Is("database=test;server=localhost;user
id=root;Password=;")))
.Mappings(m => m.AutoMappings.Add(model))
.BuildConfiguration();
new SchemaExport(cfg).Execute(false, true, false);

I'm reposting this from
http://stackoverflow.com/questions/1290466/fluent-nhibernate-duplicatemappingexception-with-automapping

thanks

James Gregory

unread,
Aug 18, 2009, 4:14:44 AM8/18/09
to fluent-n...@googlegroups.com
Looks ok to me mapping wise, I've checked the code and it should cope with classes of the same name.

Can you show the full exception?

Scott Cowan

unread,
Aug 18, 2009, 7:55:46 AM8/18/09
to Fluent NHibernate
sure here's the exception, I can create a solution with it reproduced
if that helps

FluentNHibernate.Cfg.FluentConfigurationException was unhandled
Message="An invalid or incomplete configuration was used while
creating a SessionFactory. Check PotentialReasons collection, and
InnerException for more detail.\r\n\r\n"
Source="FluentNHibernate"
StackTrace:
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration
() in c:\Documents and Settings\Administrator\dev\fluent-nhibernate\src
\FluentNHibernate\Cfg\FluentConfiguration.cs:line 121
at importer.nba.svc.Program.Main(String[] args) in C:\code
\sportinginsider\datamining\misc\TempImporter\trunk\src
\nba.importer.svc\Program.cs:line 24
at System.AppDomain._nExecuteAssembly(Assembly assembly, String
[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: NHibernate.MappingException
Message="Could not compile the mapping document: (XmlDocument)"
Source="NHibernate"
StackTrace:
at NHibernate.Cfg.Configuration.LogAndThrow(Exception
exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument
(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue
(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader
hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream
xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument
doc, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument
doc)
at FluentNHibernate.PersistenceModel.Configure
(Configuration cfg) in c:\Documents and Settings\Administrator\dev
\fluent-nhibernate\src\FluentNHibernate\PersistenceModel.cs:line 203
at
FluentNHibernate.Automapping.AutoPersistenceModel.Configure
(Configuration configuration) in c:\Documents and Settings
\Administrator\dev\fluent-nhibernate\src\FluentNHibernate\Automapping
\AutoPersistenceModel.cs:line 125
at FluentNHibernate.Cfg.AutoMappingsContainer.Apply
(Configuration cfg) in c:\Documents and Settings\Administrator\dev
\fluent-nhibernate\src\FluentNHibernate\Cfg
\AutoMappingsContainer.cs:line 64
at FluentNHibernate.Cfg.MappingConfiguration.Apply
(Configuration cfg) in c:\Documents and Settings\Administrator\dev
\fluent-nhibernate\src\FluentNHibernate\Cfg
\MappingConfiguration.cs:line 65
at
FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in c:
\Documents and Settings\Administrator\dev\fluent-nhibernate\src
\FluentNHibernate\Cfg\FluentConfiguration.cs:line 112
InnerException: NHibernate.DuplicateMappingException
Message="duplicate import: Test refers to both
Sample.Models.Test2.Test, importer.nba.svc, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null and Sample.Models.Test1.Test,
importer.nba.svc, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null (try using auto-import=\"false\")"
Source="NHibernate"
Name="Test"
Type="import"
StackTrace:
at NHibernate.Cfg.Mappings.AddImport(String
className, String rename)
at
NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindPersistentClassCommonValues
(XmlNode node, IDecoratable classMapping, PersistentClass model,
IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass
(XmlNode node, IDecoratable classMapping, PersistentClass model,
IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind
(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
at
NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode
parentNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind
(XmlNode node)
at NHibernate.Cfg.Configuration.AddValidatedDocument
(NamedXmlDocument doc)
InnerException:
> >http://stackoverflow.com/questions/1290466/fluent-nhibernate-duplicat...
>
> > thanks

James Gregory

unread,
Aug 18, 2009, 8:03:18 AM8/18/09
to fluent-n...@googlegroups.com
Hmm. From what I can gather from the exception, there's something that is referring to your class as just Test, rather than by the fully qualified name. Should just be a case of finding the place where this is happening, it's more than likely one of the other parts. A test or example project would probably help a lot.

Scott Cowan

unread,
Aug 18, 2009, 1:03:48 PM8/18/09
to Fluent NHibernate
Here's a solution showing the repro on this, (Download link is at the
bottom right)

http://www.2shared.com/file/7249183/7b1df86d/DuplicateMappingException.html

James Gregory

unread,
Aug 18, 2009, 7:07:54 PM8/18/09
to fluent-n...@googlegroups.com
Hey Scott,

Got around to having a proper look at this tonight. Basically, it is down to the AutoImport stuff the exception mentioned; when NHibernate is given the first mapping it sees that the entity is named with the full assembly qualified name and creates an import for the short name (being helpful!), and then when you add the second one it then complains that this import is now going to conflict. So the solution is to turn off the auto importing; unfortunately, we don't have a way to do that in the RC... I've just commited a fix that adds in the ability to change this in a convention.

So if you get the latest binaries or source, you should be able to change your Conventions line in your attached project to do this:

.Conventions.Setup(x =>
{
  x.AddFromAssemblyOf<Program>();
  x.Add(AutoImport.Never());
});

Which adds all the conventions you've defined in your assembly, then uses one of the helper conventions to turn off auto importing.

Scott Cowan

unread,
Aug 19, 2009, 3:32:29 AM8/19/09
to Fluent NHibernate
very cool,

thanks for that, it works great

On Aug 19, 12:07 am, James Gregory <jagregory....@gmail.com> wrote:
> Hey Scott,
> Got around to having a proper look at this tonight. Basically, it is down to
> the AutoImport stuff the exception mentioned; when NHibernate is given the
> first mapping it sees that the entity is named with the full assembly
> qualified name and creates an import for the short name (being helpful!),
> and then when you add the second one it then complains that this import is
> now going to conflict. So the solution is to turn off the auto importing;
> unfortunately, we don't have a way to do that in the RC... I've just
> commited a fix that adds in the ability to change this in a convention.
>
> So if you get the latest binaries or source, you should be able to
> change your Conventions line in your attached project to do this:
>
> .Conventions.Setup(x =>
> {
>   x.AddFromAssemblyOf<Program>();
>   x.Add(AutoImport.Never());
>
> });
>
> Which
> adds all the conventions you've defined in your assembly, then uses
> one of the helper conventions to turn off auto importing.
>
> On Tue, Aug 18, 2009 at 6:03 PM, Scott Cowan <scottjamesco...@googlemail.com
>
>
>
> > wrote:
>
> > Here's a solution showing the repro on this, (Download link is at the
> > bottom right)
>
> >http://www.2shared.com/file/7249183/7b1df86d/DuplicateMappingExceptio...
Reply all
Reply to author
Forward
0 new messages