0
votes

Before asking here I read all post relative to the subject but I can't find any solution.

I removed all my domain complexity and I'm down to the following simple issue:

I have 2 class (in a domain file Domain.cs) :

public abstract class BaseClass
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
}

public class ASubClass : BaseClass
{
    public ASubClass() {}
    public virtual string prop { get; set; }
}

My Mapping is (In another file Mapping.cs):

public class BaseClassMap : ClassMap<BaseClass>
{
    public BaseClassMap ()
    {
        Id(x => x.ID).GeneratedBy.HiLo("1000");
        Map(x => x.Name);
        DiscriminateSubClassesOnColumn("Type");
    }
}
public class ASubClassMap : SubclassMap<ASubClass>
{
    public ASubClassMap ()
    {
        Map(x => x.prop);
        DiscriminatorValue(1);
    }
}

First I let NHibernate create the DB for me :

var cfg = new NHibernate.Cfg.Configuration();
Fluently.Configure(cfg)
   .Database(MsSqlConfiguration.MsSql2008
     .ConnectionString(connectionstring)
     .ShowSql())
     .Mappings(m => m
     .FluentMappings.AddFromAssemblyOf<BaseClass>()).BuildConfiguration();
new SchemaExport(cfg).Execute(false,true,false);

This works great, then my unit test is the following : I build a session Factory within a Repository and use the Session/Transaction to try to save an empty SubClass:

var contract = new ASubClass();
IRepository<ASubClass> repository = new Repository<ASubClass>();
repository.Add(contract); >>> BUG HERE

This in fact bug on the line(inside the repo) : session.Save(contract);

I tried to copy/paste my Mapping.cs into Domain.cs or change the attribute of .FluentMappings.AddFromAssemblyOf<BaseClass> to .FluentMappings.AddFromAssemblyOf<BaseClassMap>

I also tried to change the hierarchy of subclass by removing the Discriminator and the DiscriminateSubClassesOnColumn keyword.

Didnt work so far.

Thanks.

1

1 Answers

1
votes

You code actually works fine for me.

There must be something wrong with your configuration or your repository or how you use the configuration+repository within your unit tests. I mean, you create a new Repository, does this use the configuration you posted? 100% sure?

Are you using your cfg to build the ISessionFactory to be used by your repository? e.g.

var factory = cfg.BuildSessionFactory();

Maybe check that all mappings are in the same assembly, otherwise add both assemblies.