C# 基础<;T1、T2>;其中T1:class,其中T2:class';类型应为参考…';
我不知道我在哪里搞砸了。我一直在寻找答案,但没有一个能解决我的实际问题 我的代码如下:C# 基础<;T1、T2>;其中T1:class,其中T2:class';类型应为参考…';,c#,generics,entity-framework-6,C#,Generics,Entity Framework 6,我不知道我在哪里搞砸了。我一直在寻找答案,但没有一个能解决我的实际问题 我的代码如下: namespace BusinessLogic { public abstract class CRUDBaseEntity<TDTO, TEntity> where TDTO : class where TEntity : class { public virtual TDTO GetSingle(Expression<Fun
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"));
}
}
}
DataAccess.SpecificEntity
是一个类吗?代码不编译。请显示DataAccess.SpecificEntity
的代码。您可以阅读有关类型参数约束的参考。@GillBates,DataAccess.SpecificEntity是我的实体框架数据模型的一个实体。请尝试创建一个实体,而不是在您的问题中逐段添加代码。我们无法编译您的代码。如果我尝试在“明显的”空实现中替换,那么您显示的代码不会生成您显示的错误。因此,您需要投入更多的工作来创建一个独立的示例。
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; }
}
}