我正在使用实体框架(数据库 1st),并且在将实体转换为 linq 到实体查询的一部分时遇到了一些问题,对此非常新,因此我们将不胜感激。
基本上,我有 2 个表,'tblContactTypes' 和 'tblContactDetails',如果我返回一组联系人详细信息,如何为每个表返回一个联系人类型?我可以返回 tblContactType 类的实例,但我不确定如何返回 Contact Type 对象。
我的课程如下(我要整理的行是 GetAll() 方法中的'ContactType = x.tblContactType') -
数据层 -
public partial class tblContactType
{
public tblContactType()
{
this.tblContactDetails = new HashSet<tblContactDetail>();
}
public int ContactTypeID { get; set; }
public string Name { get; set; }
public string ExpressionValidator { get; set; }
public virtual ICollection<tblContactDetail> tblContactDetails { get; set; }
}
public partial class tblContactDetail
{
public tblContactDetail()
{
this.tblCustomerContactDetails = new HashSet<tblCustomerContactDetail>();
}
public int ContactDetailID { get; set; }
public string ContactDetail { get; set; }
public int ContactTypeID { get; set; }
public virtual tblContactType tblContactType { get; set; }
public virtual ICollection<tblCustomerContactDetail> tblCustomerContactDetails { get; set; }
}
业务层 -
public class ContactTypeVO
{
public int ContactTypeID { get; set; }
public string Name { get; set; }
public string ExpressionValidator { get; set; }
/// <summary>
/// Determines whether this instance is new.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is new; otherwise, <c>false</c>.
/// </returns>
public bool IsNew()
{
return ContactTypeID == 0;
}
}
public class ContactDetailVO
{
public int ContactDetailID { get; set; }
public string ContactDetail { get; set; }
public int ContactTypeID { get; set; }
public tblContactType ContactType { get; set; }
public ICollection<tblCustomerContactDetail> CustomerContactDetails { get; set; }
/// <summary>
/// Determines whether this instance is new.
/// </summary>
/// <returns>
/// <c>true</c> if this instance is new; otherwise, <c>false</c>.
/// </returns>
public bool IsNew()
{
return ContactDetailID == 0;
}
}
public class ContactDetailDAO : IDisposable
{
/// <summary>
/// The repository
/// </summary>
public IContactDetailsRepository repository;
/// <summary>
/// Initializes a new instance of the <see cref="ContactDetailEO" /> class.
/// </summary>
public ContactDetailDAO()
{
repository = new ContactDetailsRepository();
}
/// <summary>
/// Saves the specified contact detail.
/// </summary>
/// <param name="contactDetail">The contact detail.</param>
public void Save(ContactDetailVO contactDetail)
{
if (this.IsValid())
{
if (contactDetail.IsNew())
{
repository.Add(new tblContactDetail
{
ContactDetailID = contactDetail.ContactDetailID,
ContactDetail = contactDetail.ContactDetail,
ContactTypeID = contactDetail.ContactTypeID
});
}
else
{
var saveItem = repository.AsQueryable().Where(x => x.ContactDetailID == contactDetail.ContactDetailID).FirstOrDefault();
if (saveItem != null)
{
saveItem.ContactDetailID = contactDetail.ContactDetailID;
saveItem.ContactDetail = contactDetail.ContactDetail;
saveItem.ContactTypeID = contactDetail.ContactTypeID;
repository.Update(saveItem);
}
}
}
else
{
// ERROR
}
}
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
public List<ContactDetailVO> GetAll()
{
var contacts = repository.AsQueryable()
.Select(x => new ContactDetailVO
{
ContactDetailID = x.ContactDetailID,
ContactDetail = x.ContactDetail,
ContactTypeID = x.ContactTypeID,
ContactType = x.tblContactType,
});
return contacts.ToList();
}
/// <summary>
/// Gets the by ID.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public ContactDetailVO GetByID(int id)
{
var contactDetail = repository.AsQueryable().Where(x => x.ContactDetailID == id).FirstOrDefault();
if (contactDetail == null)
return null;
return new ContactDetailVO
{
ContactDetailID = contactDetail.ContactDetailID,
ContactDetail = contactDetail.ContactDetail,
ContactTypeID = contactDetail.ContactTypeID,
ContactType = contactDetail.tblContactType
};
}
/// <summary>
/// Determines whether this instance is valid.
/// </summary>
public bool IsValid()
{
return true;
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
}
}