问题描述
我从数据库生成Linq-to-Entity模型并对其进行了修改 - 我创建了接口:
public interface IValid
{
字节有效{get;组; }
}
并使一些生成的类继承此接口。
我写了泛型类来访问数据库中的表:
公开列表< T> GetValidRecords< T>()其中T:class,IValid
{
try
{
return _context.Set< T>()。其中(x => x.Valid == 1).ToList();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
当我在单元测试中调用此方法时, p>
var records = repositary.GetValidRecords< tableName>();
我得到错误 -
如何解决它?
编辑:
my table class:
public partial class tableName:IValid {
public byte IsValid {get;组; }
}
编辑2:
我的调用方法:
public void GetValidRecordsGenericTest()
{
var data = new List< tableName>
{
new tableName(){Valid = 1},
new tableName(){Valid = 1}
} .AsQueryable();
var mockSet = new Mock< DbSet< tableName>>();
mockSet.As< IQueryable< tableName>>()。Setup(m => m.Provider).Returns(data.Provider);
mockSet.As< IQueryable< tableName>>()。Setup(m => m.Expression).Returns(data.Expression);
mockSet.As< IQueryable< tableName>>()。Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As< IQueryable< tableName>>()。Setup(m => m.GetEnumerator())。 var mockContext = new Mock< Entities>();
mockContext.Setup(x => x.tableNames).Returns(mockSet.Object);
var database = new Database(mockContext.Object);
var records = database.GetValidRecords< tableName>(); //这里我得到错误
Assert.AreEqual(2,records.Count,错误的gueltig记录数。
class tableName:IValid
应该是这样的: {
//实现IValid
}
还要确保类 tableName
实现与方法 GetValidRecords
中使用的相同的IValid接口,即 IValid
来自正确的命名空间。
I generated Linq -to -Entity model from database and modified it - I've made interface :
public interface IValid
{
byte Valid{ get; set; }
}
and make some generated classes to inherit this interface.
I've wrote generic class to access tables from database:
public List<T> GetValidRecords<T>() where T: class, IValid
{
try
{
return _context.Set<T>().Where(x => x.Valid == 1).ToList();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
When I call this method in my unit test
var records = repositary.GetValidRecords<tableName>();
I get error -
How to fix it?
EDIT:my table class:
public partial class tableName: IValid {
public byte IsValid { get; set; }
}
EDIT2:My calling method:
public void GetValidRecordsGenericTest()
{
var data = new List<tableName>
{
new tableName() {Valid = 1},
new tableName() {Valid = 1}
}.AsQueryable();
var mockSet = new Mock<DbSet<tableName>>();
mockSet.As<IQueryable<tableName>>().Setup(m => m.Provider).Returns(data.Provider);
mockSet.As<IQueryable<tableName>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<tableName>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<tableName>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); var mockContext = new Mock<Entities>();
mockContext.Setup(x => x.tableNames).Returns(mockSet.Object);
var database = new Database(mockContext.Object);
var records = database.GetValidRecords<tableName>(); // here I get error
Assert.AreEqual(2, records.Count, "Wrong number of gueltig records.");
}
tableName
should be something like this for it to work:
class tableName : IValid
{
// implement IValid
}
Also make sure that the class tableName
implements the same IValid interface as used in the method GetValidRecords
, i.e. the IValid
from the correct namespace.
这篇关于类型''不能用作泛型类型或方法''中的类型参数'T'。没有从''到''的隐式引用转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!