nhibernate / NHibernate.AspNetCore.Identity

ASP.NET Core Identity Provider for NHibernate
GNU Lesser General Public License v2.1
64 stars 13 forks source link

No persister for: NHibernate.AspNetCore.Identity.IdentityUserRole #13

Closed Elvin1492 closed 4 years ago

Elvin1492 commented 4 years ago

I am getting this issue when trying to add role to user I am using fluent mapping as follow, table was successfully created. Also the user and the role is in database already. So they were created successfully.

 public class AppUserRoleMap : ClassMapping<AppUserRole>
    {
        public AppUserRoleMap()
        {
            Table("AspNetUserRoles");
            ComposedId(x =>
            {
                Property(y => y.UserId, y =>
                {
                    y.Type(NHibernateUtil.String);
                    y.Length(32);
                    y.Column("UserId");
                });

                Property(y => y.RoleId, y =>
                {
                    y.Type(NHibernateUtil.String);
                    y.Length(32);
                    y.Column("RoleId");
                });
            });
        }
    }
beginor commented 4 years ago

Because of the issue with fluent-nhibernate, it seems joined-subclass mapping is not supported by FluentNHibernate now.

  1. Create all mappings for all classes, include NHibernate.AspNetCore.Identity 's classes , please refer to the exists hbm mappings;
  2. Just use *.hbm.xml files , don't use FluentHiberate;
  3. Use NHibernate.Mapping.Attributes, it support joined-subclass mapping with the existing hbm xml mapping , which I have tested with it before。

Personaly, I prefer to use xml mapping (*.hbm.xml), because both FluentHiberate and NHibernate.Mapping.Attributes will translate it's mapping to xml mapping at runtime. And with the help of NHibernate's xml schema (nhibernate-configuration.xsd and nhibernate-mapping.xsd) file, editing hbm file with intelli popup is easy too.

For more details, please refer to issue #10 .

marcelNgan commented 4 years ago

@beginor have you tried using Nhibernate.Mapping.ByCode? Have the "root entity for AppRole was never registered" MappingException even when using JoinedSubclassMapping. Would love a confirmation or a way around it.

Elvin1492 commented 4 years ago

@marcelNgan I have already developed fluent-mapping for identity. You can find it in this repo. @beginor , if you want I can create PR for it in here.

beginor commented 4 years ago

@Elvin1492 Thanks, but I do not use fluent-mapping because of https://github.com/nhibernate/NHibernate.AspNetCore.Identity/issues/13#issuecomment-546645665

beginor commented 4 years ago

@marcelNgan I will try using this library with NHibernate.Mapping.ByCode , But I think it should be the same as NHibernate.Mapping.Attributes, which I had test before.

beginor commented 4 years ago

@marcelNgan If you are using with NHibernate.Mapping.ByCode , please start with rewriting of AppRole.hbm.xml and like this :

using NHibernate;
using NHibernate.Type;
using NHibernate.Mapping.ByCode.Conformist;
using NHIdentityRole = NHibernate.AspNetCore.Identity.IdentityRole;

namespace WebTest.Entities {

    public class AppRoleMapping : JoinedSubclassMapping<AppRole> {

        public AppRoleMapping() {
            // Add NHIdentityRole as root entity first
            ExplicitDeclarationsHolder.AddAsRootEntity(typeof(NHIdentityRole));
            Extends(typeof(NHIdentityRole));
            Schema("public");
            Table("app_roles");
            Key(k => k.Column("id"));
            Property(
                p => p.Description,
                maping => {
                    maping.Column("description");
                    maping.Type(NHibernateUtil.String);
                    maping.Length(256);
                }
            );
        }

    }

}

Then compile the mapping and add them after AddIdentityMappings in Startup.cs

var cfg = new Configuration();
var file = Path.Combine(
    AppDomain.CurrentDomain.BaseDirectory,
    "hibernate.config"
);
cfg.Configure(file);
// Add identity mapping based on dialect config (dialet must contains
// PostgreSQL, MySQL or MsSql)
cfg.AddIdentityMappings();
// cfg.AddAssembly(typeof(Startup).Assembly);
var modelMapper = new NHibernate.Mapping.ByCode.ModelMapper();
modelMapper.AddMapping<AppRoleMapping>();
modelMapper.AddMapping<AppUserMapping>();
modelMapper.AddMapping<TodoItemMapping>();
var mappings = modelMapper.CompileMappingForAllExplicitlyAddedEntities();
cfg.AddMapping(mappings);
beginor commented 4 years ago

@Elvin1492 I think the project offluent-mapping is obsoleted, does not have any update for more then one year.

if you are interested in using of NHibernate.Mapping.ByCode, please keep track of #16 , I will refact to use NHibernate.Mapping.ByCode.