views:

535

answers:

2

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

+2  A: 

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.

Scott Cowan
This advice works for non-automatic mapping as well.
ddc0660
A: 

I am having real problem with this, and the example above or any of its variants do not help.

var cfg = new NotifyFluentNhibernateConfiguration();

    return Fluently.Configure()
      .Database(
       FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
            .ConnectionString("Server=10.2.65.227\\SOSDBSERVER;Database=NotifyTest;User ID=NHibernateTester;Password=test;Trusted_Connection=False;")
      )

      .Mappings(m => {
          m.AutoMappings
            .Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg));
          m.FluentMappings.Conventions.Setup(x =>
          {
              x.AddFromAssemblyOf<Program>();
              x.Add(AutoImport.Never());
          });
      } )

      .BuildSessionFactory();

I can't find Program's reference..

I've also tried to put down a seperate xml file to in desperation config fluent nhibernate's mapping to auto-import = false with no success.

Can I please have some more extensive example on how to do this?

Edit, I got the latest trunk just weeks ago.

Edit, Solved this by removing all duplicates.

Simply G.