views:

97

answers:

2

My User table I want to map to aspnet_Users:

<class name="User" table="`User`">
    <id name="ID" column="ID" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <property name="UserId" column="UserId" type="Guid" not-null="true" />
    <property name="FullName" column="FullName" type="String" not-null="true" />
    <property name="PhoneNumber" column="PhoneNumber" type="String" not-null="false" />
</class>

My aspnet_Users table:

<class name="aspnet_Users" table="aspnet_Users">
    <id name="ID" column="UserId" type="Guid" />
    <property name="UserName" column="UserName" type="string" not-null="false" />
</class>

I tried adding one-to-one, one-to-many and many-to-one mappings. The closest I can get is with this error: Object of type 'System.Guid' cannot be converted to type 'System.Int32'.

How do I create a 1 way mapping from User to aspnet_User via the UserId column in User?

I am only wanting to create a reference so I can extract read-only information, affect sorts, etc. I still need to leave UserId column in User set up like it is now. Maybe a virtual reference keying off of UserId? Is this even possible with Nhibernate?

Unfortunately it acts like it only wants to use ID from User to map to aspnet_Users. Changing the table User to have it's primary key be UserId instead of ID is not an option at this point.

Any help would be greatly appreciated. Thanks in advance.

A: 

Aspnet_user table has a primary key of type Guid.

Your table User has a primary key of type Int32.

You can look at this as your user table has a reference (one to one) to aspnet_users table.

But if you look at the aspnet_users table as if it is a 'basket of values' to assign to a user (like if you'd have a type of user (admin, moderator, ...), but that would be many-to-one refrence (many users have one of the type) you can map it like that.

So:

class User
{
     public virtual int ID { get; set; }
     public virtual Guid UserId { get; set; }
     public virtual string FullName { get; set; }
     public virtual string PhoneNumber { get; set; }   
}

class AspnetUser
{
     public virtual Guid ID { get; set; }
     public virtual string UserName { get; set; }
}

mapping would be (using fluent.NHibernate):

  public class User_Map : ClassMap<User>
  {
      public User_Map()
      {
           Table("User");
           ID(x => x.ID).GeneratedBy.Native();
           References(x => x.UserId);
           Map(x => x.FullName);
           Map(x => x.PhoneNumber);
      }
  }

  public class AspnetUser_Map : ClassMap<AspnetUser>
  {
      public User_Map()
      {
           Table("aspnet_Users");
           ID(x => x.ID).GeneratedBy.Assigned();
           Map(x => x.UserName);
      }
  }
dmonlord
Thanks. It worked.
Michael D. Kirkpatrick
A: 

To get some further guidance on this, have a look at the NHibernateProvider project. They have created a full membership provider implementation that uses NHibernate under the hood.

DanP
Thanks for pointing this out. I will take a look at it.
Michael D. Kirkpatrick