Search code examples
c#nhibernatefluent-nhibernateasp.net-identityfluent-nhibernate-mapping

NHibernate.AspNet.Identity FluentNhibernate - how do I add a custom object to my ApplicationUSer?


I am very, very new to Nhibernate and I am learning on the go (painfully).

I am trying to use NHibernate with Asp .NET Identity 2.0 and set my own store. However, I am having issues -

Though I understand mappings between entities, I can't seem to create relevant mappings between entities. Here is what I have. An ApplicationUser class and setup, similar to : https://github.com/nhibernate/NHibernate.AspNet.Identity

public class ApplicationUser : IdentityUser
{
    public virtual IList<Character> Characters { get; set; }

    public ApplicationUser()
    {
        Characters = new List<Character>();
    }
}

And a character class

public class Character
{
    public virtual Guid Id { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

And here is my main configuration section:

var configuration = Fluently.Configure()
            .Database(
                MsSqlConfiguration.MsSql2012.ConnectionString(
                    "server=localhost;database=SWDB;trusted_connection=true;"))
            .Mappings(x=>x.FluentMappings.AddFromAssemblyOf<CharacterMapping>())
            .ExposeConfiguration(cfg =>
            {
                cfg.SetProperty("current_session_context_class", "web");
                cfg.AddDeserializedMapping(
                    MappingHelper.GetIdentityMappings(myEntities), null);
                new SchemaExport(cfg).Create(false, true);
            }).BuildConfiguration();

Finally the mappings:

public class ApplicationUserMapping : SubclassMap<ApplicationUser>
{
    public ApplicationUserMapping()
    {
        HasMany(x => x.Characters).Inverse().Cascade.All();
    }
}

and

public class CharacterMapping : ClassMap<Character>
{
    public CharacterMapping()
    {
        Id(x => x.Id);
        References(x => x.ApplicationUser).Cascade.All();
    }
}

However, whenever I try to create and save a character, I get: Could not determine type for: SWDB.Domain.RP.Character, SWDB.Domain, for columns: NHibernate.Mapping.Column(id)

I've been battling this all day - I give up :( any help is appreciated...

Thanks !


Solution

  • Your Id is a Guid which is not supported by Identity. Specify the generator manually

    public class CharacterMapping : ClassMap<Character>
    {
        public CharacterMapping()
        {
            Id(x => x.Id).GeneratedBy.GuidComb();
            References(x => x.ApplicationUser).Cascade.All();
        }
    }