2


I Want to create table (SQL Server) that contains users identity GUID and make reference to it in Fluent NHibernate, here is my model:

public class Invoice {
    public virtual Guid Identity { get; set; }
    public virtual MembershipUser User { get; set; }
    public virtual int Price { get; set; }
}

So the mapping should be:

public class InvoiceMap : ClassMap<Invoice> {
    public InvoiceMap() {
        Id(x => x.Identity).GeneratedBy.GuidNative();
        Reference(x => x.User).Column("User");
        Map(x => x.Price);
        Table("invoices");
    }
}

But there are only one problem. The class MembershipUser also should be mapped.
How I can do this without mapping MembershipUser?

Zilberman Rafael
  • 1,341
  • 2
  • 14
  • 45

1 Answers1

2

I guess your question is "how to use MembershipUser/Membership and Role Provider in nHibernate".

First of all, if you don't want to reinvent the wheel, I'd suggest to remove the MembershipUser property from your class and just put a reference to some identifier. Otherwise it is getting more complex. Later you can convert/retrieve the MembershipUser from your MembershipProvider...

There is one good article on codeplex describing how to implement a custom membership provider with nHibernate http://www.codeproject.com/Articles/55174/Custom-Fluent-Nhibernate-Membership-and-Role-Provi

MichaC
  • 13,104
  • 2
  • 44
  • 56
  • Is there any way to do something like this: `Reference(x => x.User).Column("User").Callback(x => MembershipProvider.GetUserById(x));` ? – Zilberman Rafael Sep 26 '13 at 20:42
  • No, but I mean you can put a property into you entity which is not mapped (of type MembershipUser) and set it manually – MichaC Sep 26 '13 at 20:45
  • btw you could mark this as answered and ask a more concrete question if you still have an issue, because this one was a bit vague – MichaC Sep 27 '13 at 07:17
  • Before marking, you mean I can do something like this: `Map(x => x.User); this.Member = MembershipProcider.GetUserById(this.User);`? – Zilberman Rafael Sep 27 '13 at 07:36
  • no not in the mapping, but one possibility would be to change the setter of the User property in your entity and also set the MembershipUser ;) Only at this point you actually have the information. The mapping file just defines meta data, this is no place for any business logic – MichaC Sep 27 '13 at 07:39
  • Rafael poor form really, as @ela has gone to the trouble and answered your question and you are in fact asking something completely different... +1 for me as you can set the User property later in the chain, just mark (keep) it as Virtual – Rippo Sep 27 '13 at 11:22