首先,抱歉文字墙。
我有以下架构。请注意,User_Contact
中的ContactID
可能包含空值。
在课程级别,然后按以下方式实施
public abstract class User : EntityBase<Guid>, IAggregateRoot
{
public User()
{
}
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
protected abstract override void Validate();
}
public class SiteUser : User
{
public SiteUser() { }
public virtual Guid ApplicationId { get; set; }
public virtual string UserName { get; set; }
public virtual string LoweredUserName { get; set; }
public virtual string MobileAlias { get; set; }
public virtual bool IsAnonymous { get; set; }
public virtual DateTime LastActivityDate { get; set; }
protected override void Validate()
{
if (this.ApplicationId == Guid.Empty)
base.AddBrokenRule(UserBusinessRules.ApplicationIdRequired);
if (this.UserName.IsNullOrEmpty())
base.AddBrokenRule(UserBusinessRules.UserNameRequired);
if (this.LoweredUserName.IsNullOrEmpty())
base.AddBrokenRule(UserBusinessRules.LoweredUserNameRequired);
if (this.LastActivityDate == DateTime.MinValue)
base.AddBrokenRule(UserBusinessRules.LastActivityDateRequired);
}
}
public class SiteContact : SiteUser
{
public SiteContact()
{
}
public virtual int ExternalID { get; set; }
//All the rest...
protected override void Validate()
{
//validate
}
}
所以基本上这个想法是每一个SiteContact
都是SiteUser
但不是每个SiteUser
都是SiteContact
。
我完全陷入困境的是如何在nHibernate中映射这种关系。我似乎能够检索SiteUser
但无法检索SiteContact
,这很清楚原因。我实现的映射导致执行以下sql。
SELECT this_.userid AS UserId18_0_,
this_.firstname AS FirstName18_0_,
this_.lastname AS LastName18_0_,
//Blah blah.....
FROM user_contact this_
INNER JOIN contact this_1_
ON this_.userid = this_1_.contactid //Here is the error this should be ContactID = ContactID
INNER JOIN aspnet_users this_2_
ON this_.userid = this_2_.userid
WHERE ( CASE
WHEN this_.contactid IS NOT NULL THEN 1
ELSE 0
END ) = '1'
但我不能为我的生活解决如何在我的hbm映射文件中解决这个问题。为简单起见,这里省略了一些字段。
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyProject.Namespaces.Model.Entities" assembly="MyProject.Namespaces.Model">
<class name="User" table="User_Contact" lazy="true" abstract="true">
<id name="ID" column="UserId" type="guid">
<generator class="guid" />
</id>
<discriminator column="ContactID" formula="(CASE WHEN ContactID IS NOT NULL THEN 1 ELSE 0 END)" />
<property name="FirstName">
<column name="FirstName" sql-type="nvarchar(500)" not-null="true" />
</property>
<property name="LastName">
<column name="LastName" sql-type="nvarchar(500)" not-null="true" />
</property>
<subclass name="SiteContact" discriminator-value="1">
<join table="Contact">
<key column="ContactID" /> //I assume the problem is here. I have tried adding foreign-key="ContactID" to no success
<property name="ExternalID" insert="false" update="false">
<column name="ExternalID" sql-type="int" not-null="true" />
</property>
//The rest of the mapped columns
</join>
<join table="aspnet_Users">
<key column="UserId" />
<property name="ApplicationId">
<column name="ApplicationId" sql-type="guid" not-null="true" />
</property>
<property name="UserName">
<column name="UserName" sql-type="nvarchar(256)" not-null="true" />
</property>
<property name="LoweredUserName">
<column name="LoweredUserName" sql-type="nvarchar(256)" not-null="true" />
</property>
<property name="MobileAlias">
<column name="MobileAlias" sql-type="nvarchar(16)" not-null="false" />
</property>
<property name="IsAnonymous">
<column name="IsAnonymous" sql-type="bit" not-null="true" />
</property>
<property name="LastActivityDate">
<column name="LastActivityDate" sql-type="datetime" not-null="true" />
</property>
</join>
</subclass>
<subclass name="SiteUser" discriminator-value="0">
<join table="aspnet_Users">
<key column="UserId" />
//blah blah blah
</join>
</subclass>
</class>
</hibernate-mapping>
希望有人可以告诉我,如果我在关于这种映射的问题上走得正确,可能会提供一些指导吗?
答案 0 :(得分:1)
这似乎是一种非常复杂的映射方式。我不确定在这种情况下你希望通过使用继承来实现什么好处,我建议使用组合(即每个表有一个类并映射关系)。
话虽如此,你可能有你的理由,所以我建议你做以下我想你想要的事情: