将映射的业务对象返回到EF Entity分部类时出错

时间:2013-02-26 15:39:12

标签: c# entity-framework

我正在尝试将业务对象映射到数据优先,自动生成的实体。但是,Iam在我的mapper类中收到错误,我返回new Lab

错误为"Cannot Convert expression type 'LabManager.DataAcces.Lab' to return type LabManager.BusinessObjects.BusinessObjects.Lab"

我的问题是:当我在mapper类中完全按照预期返回时,为什么会出现此错误?

我的业务对象如下所示:

namespace LabManager.BusinessObjects.BusinessObjects
{
    public class Lab
    {
        public Lab()
        {

        }
        public int Id { get; set; }
        public string Name { get; set; }
        public IList<Cylinder> Cylinders { get; set; }
    }
}

我将业务对象映射到的实体是:

public partial class Lab
{
    public Lab()
    {
        this.Cylinders = new HashSet<Cylinder>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Cylinder> Cylinders { get; set; }
}

我只是使用手动滚动的mapper类(没有AutoMapper):

namespace EmitLabManager.DataAccess.ModelMapper
public class Mapper
{
   internal static BusinessObjects.BusinessObjects.Lab GetLabs(Lab entity)
   {
        return new Lab
        {
             Id = entity.Id,
             Name = entity.Name,
             Cylinders = entity.Cylinders
        };
    }
}

1 个答案:

答案 0 :(得分:1)

您很可能发生命名空间冲突。您需要在GetLabs函数中完全限定构造函数:

return new BusinessObjects.BusinessObjects.Lab
    {
         Id = entity.Id,
         Name = entity.Name,
         Cylinders = entity.Cylinders
    };

这应该可以解决问题。