Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate DuplicateMappingException with AutoMapping

Summary:

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

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);

Thanks I really appreciate any help

Update using Fluent Nhibernate RC1

like image 514
Scott Cowan Avatar asked Dec 10 '22 19:12

Scott Cowan


2 Answers

solution from fluent-nhibernate forums by James Gregory

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.

like image 59
Scott Cowan Avatar answered Dec 31 '22 15:12

Scott Cowan


I was not able to get this to work using Conventions for FluentMappings (in contrast to AutoMappings). However, the following works for me, though it must be added to each ClassMap where needed.

public class AMap : ClassMap<A> 
{
    public AMap()
    {
        HibernateMapping.Not.AutoImport();
        Map(x => x.Item, "item");
        ...
    }
}
like image 25
Scott Moorhouse Avatar answered Dec 31 '22 16:12

Scott Moorhouse