Base <t1,t2 =“”>其中T1:class其中T2:class&#39;类型mus是参考...&#39;

时间:2017-06-27 10:03:22

标签: c# generics entity-framework-6

我不知道我在哪里搞砸了。一直在寻找答案但没有解决我的实际问题。

我的代码如下:

namespace BusinessLogic
{
    public abstract class CRUDBaseEntity<TDTO, TEntity>
        where TDTO : class
        where TEntity : class
    {
        public virtual TDTO GetSingle(Expression<Func<TDTO, bool>> where)
        {
            TDTO item = null;
            using (var context = new MyEntities())
            {
                Func<TDTO, bool> delegated = where.Compile();
                Func<TEntity, bool> destWere = d => delegated(MapDTOFromEntity(d));
                IQueryable<TEntity> dbQuery = context.Set<TEntity>();

                var fetch = dbQuery
                    .AsNoTracking()
                    .FirstOrDefault(destWere); 
                if (fetch == null) return null;
                item = MapDTOFromEntity(fetch);
            }
            return item;
        }        
    }

    public class Specific : CRUDBaseEntity<DTO.DTOSpecific, DataAccess.SpecificEntity>
    {

    }
}

namespace Client
{
    public class ClientClass
    {
        public void Caller()
        {
            var fetch = new BusinessLogic.Specific().GetSingle(f => f.SomeProperty.Equals("someValue"));
        }
    }
}

结果,我得到了:

  

&#34;类型&#39; DataAccess.SpecificEntity&#39;必须是参考类型才能将其用作参数&#39; TEntity&#39;通用类型或方法&#39; BusinessLogic.CRUDBaseEntity<TDTO,TEntity>&#39;

显然,DTO没有问题,因为它在客户端被引用。 请注意,BusinessLogic,DataAccess和Client是同一解决方案的3个不同项目。

编辑: SpecificEntity是一个EntityFramework自动生成的类,表示来自我的数据库的实体。很明显:

namespace DataAccess
{
    using System;
    using System.Collections.Generic;

    public partial class SpecificEntity
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public SpecificEntity()
        {
            this.SpecificEntityPriceList = new HashSet<SpecificEntityPriceList>();
        }

        public string SpecificEntityId { get; set; }
        public string Description { get; set; }
        public bool IsActive { get; set; }

        public virtual SpecificEntityCategory SpecificEntityCategory { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SpecificEntityPriceList> SpecificEntityPriceList { get; set; }
    }
} 

0 个答案:

没有答案