我有以下实体和实体属性:
public class UserContact
{
public virtual ulong ContactId {get; set;}
public virtual ulong UserId {get; set;}
public virtual ulong ContactUserId {get; set;}
public virtual string ContactFirstNm {get; set;}
public virtual string ContactLastNm {get; set;}
}
我的数据库架构包含以下带有列的视图和表:
CONTACT_VIEW
CONTACT_ID
USER_ID
CONTACT_USER_ID
USERS
USER_ID
FIRST_NM
LAST_NM
我正在尝试创建我的映射类,以便我可以将CONTACT_VIEW.CONTACT_USER_ID上的视图和表连接到USERS.USER_ID。但是,我不知道如何进行连接映射。
public UserContactMap()
{
Table("CONTACT_VIEW");
CompositeId()
.KeyProperty(x => x.ContactId, "CONTACT_ID")
.KeyProperty(x => x.UserId, "USER_ID");
Map(x => x.ContactUserId, "CONTACT_USER_ID");
Join("USERS", jx =>
{
//what do i put here?
jx.Map(x => x.ContactFirstNm, "FIRST_NM");
jx.Map(x => x.ContactLastNm, "LAST_NM");
};
}
修改
通过修改我的实体(以及创建另一个实体),我能够实现与原始目标相同的效果。但是,我不相信没有办法像上面的布局方式那样做。
public class UserContact
{
public virtual ulong ContactId {get; set;}
public virtual ulong UserId {get; set;}
public virtual ulong ContactUserId {get; set;}
public virtual UserContactDetail ContactDetail {get; set;}
}
public class UserContactDetail
{
public virtual ulong UserId {get; set;}
public virtual string FirstNm {get; set;}
public virtual string LastNm {get; set;}
}
//map constructor
public UserContactMap()
{
Table("CONTACT_VIEW");
CompositeId()
.KeyProperty(x => x.ContactId, "CONTACT_ID")
.KeyProperty(x => x.UserId, "USER_ID");
Map(x => x.ContactUserId, "CONTACT_USER_ID");
References(x => x.ContactDetail , "CONTACT_USER_ID");
}
//map constructor
public ContactDetailMap()
{
Table("USERS");
Id(x => x.UserId, "USER_ID");
Map(x => x.FirstNm, "FIRST_NM");
Map(x => x.LastNm, "LAST_NM");
}
有没有人知道更优雅的解决方案(只需要连接两个表来创建单个实体)。我不喜欢这样一个事实,即我必须创建另一个实体才能使我的原始实体“正常工作”。