链接到实体-强制转换表对象

本文关键字:转换 对象 实体 链接 | 更新日期: 2023-09-27 18:05:38

我正在使用实体框架(数据库1),我有几个问题,铸造实体作为链接到实体查询的一部分,非常新的,所以任何帮助将不胜感激。

基本上,我有2个表,'tblContactTypes'和'tblContactDetails',如果我返回一组联系人详细信息,我怎么能返回每个联系人类型?我可以返回一个tblContactType类的实例,但是我不确定如何返回一个ContactType对象。

我的类如下(我试图排序的行是'ContactType = x.tblContactType'在GetAll()方法)-

数据层-

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()
    {
    }
}

链接到实体-强制转换表对象

您需要手动构建您的ContactTypeVO对象实例。x.tblContactType是tblContactType类型的实例,它不会神奇地转换为ContactTypeVO的实例。

我的建议是实现一个ContactTypeVO构造函数,它接受一个tblContactType的实例并适当地设置属性,或者实现一个隐式强制转换操作符来做同样的事情。

构造函数方法的示例:

public ContactTypeVO( tblContactType object )
{
    ContactTypeId = object.ContactTypeId;
    <etc> = object.<etc>
}

然后将ContactType = x.tblContactType更改为ContactType = new contactttypevo (x.tblContactType)