Search code examples
fluent-nhibernatenhibernate-mappingbreeze

How to implement Asp.net IdentityUser with breezejs NHibernate


I am working with a project that leverage Breezejs and NHibernate. I implemented Asp.Net IdentityUser in my entity model. Anytime i tried to generate metedata, insert or update my model, using breeze NHContext. a foreign key not match exception is always thrown. Please, how do i use Fluent mapping in my code in order get over this NorthBreeze limitation


Solution

  • When using NHibernate with Breeze, the foreign keys must be mapped to properties of your entity class. That is so the foreign keys can will be available on the client. For the IdentityUserClaim entity, you would need something like this:

    public class IdentityUserClaim : EntityWithTypedId<int>
    {
        public virtual string ClaimType { get; set; }
        public virtual string ClaimValue { get; set; }
        public virtual IdentityUser User { get; set; }
    
        // foreign key property
        public virtual int UserId { get; set; } 
    }
    
    public class IdentityUserClaimMap : ClassMapping<IdentityUserClaim>
    {
        public IdentityUserClaimMap()
        {
            Table("AspNetUserClaims");
            Id(x => x.Id, m => m.Generator(Generators.Identity));
            Property(x => x.ClaimType);
            Property(x => x.ClaimValue);
            ManyToOne(x => x.User, m => m.Column("User_Id"));
    
            // foreign key mapping
            Property(x = x.UserId).Column("User_Id").Not.Insert().Not.Update();
        }
    }
    

    Note the foreign key is mapped with insert=false and update=false. Updates to the User_Id column go through the normal NHiberate flow (i.e. they are controlled by the related User entity). The UserId property is used only to expose the foreign key value to Breeze.