C# 抑制状态错误CS1936找不到源类型“int”的查询模式的实现选择“未找到”
在过去的几个小时里,我一直在为这个错误绞尽脑汁,请在将它标记为副本之前,请仔细阅读这个问题,我和其他人都知道这可能被标记为副本,我甚至遇到了我自己多年前提出的问题,当时的解决方案是删除我的EDMX并重新生成它 找不到源类型“int”的查询模式的实现选择“未找到” 我在SSMS中编写了一个存储过程:C# 抑制状态错误CS1936找不到源类型“int”的查询模式的实现选择“未找到”,c#,entity-framework,C#,Entity Framework,在过去的几个小时里,我一直在为这个错误绞尽脑汁,请在将它标记为副本之前,请仔细阅读这个问题,我和其他人都知道这可能被标记为副本,我甚至遇到了我自己多年前提出的问题,当时的解决方案是删除我的EDMX并重新生成它 找不到源类型“int”的查询模式的实现选择“未找到” 我在SSMS中编写了一个存储过程: create procedure usp_GetMaterialByVendorID (@VendorID int) AS SET NOCOUNT OFF SET TRANSA
create procedure usp_GetMaterialByVendorID
(@VendorID int)
AS
SET NOCOUNT OFF
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
DECLARE @ERROR_SEVERITY int,
@MESSAGE varchar(1000),
@ERROR_NUMBER int,
@ERROR_PROCEDURE nvarchar(200),
@ERROR_LINE int,
@ERROR_MESSAGE nvarchar(4000);
begin try
select
mat.MaterialID,
mcat.MaterialCategoryID,
mcat.MaterialCategory,
mtype.MaterialType,
mstype.MaterialSubType,
mat.Description,
mcost.Cost,
mat.NewPrice,
mat.RemodelPrice
from
[Material] mat
inner join
[MaterialCategory] mcat on mcat.MaterialCategoryID = mat.MaterialCategoryID
inner join
[MaterialType] mtype on mtype.MaterialTypeID = mat.MaterialTypeID
inner join
[MaterialSubType] mstype on mstype.MaterialSubTypeID = mat.MaterialSubTypeID
inner join
[MaterialCost] mcost on mcost.MaterialID = mat.MaterialID
where
mat.VendorID = @VendorID
end try
BEGIN CATCH
SET @ERROR_SEVERITY = ISNULL(ERROR_SEVERITY(),'');
SET @ERROR_NUMBER = ISNULL(ERROR_NUMBER(),'');
SET @ERROR_PROCEDURE = ISNULL(ERROR_PROCEDURE(),'');
SET @ERROR_LINE = ISNULL(ERROR_LINE(),'');
SET @ERROR_MESSAGE = ISNULL(ERROR_MESSAGE(),'');
-- Test if the transaction is uncommittable.
IF (XACT_STATE()) = -1
BEGIN
--PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
ROLLBACK TRANSACTION;
END;
-- Test if the transaction is active and valid.
IF (XACT_STATE()) = 1
BEGIN
--PRINT N'The transaction is committable. Committing transaction.'
COMMIT TRANSACTION;
END;
SET @MESSAGE = 'Error occurred in stored procedure ' + cast(@ERROR_PROCEDURE as varchar(200)) +
'; Line Number ' + cast(@ERROR_LINE as varchar) +
'; Message: [' + cast(@ERROR_NUMBER as varchar) + '] - '
+ cast(@ERROR_MESSAGE as varchar(255))
RAISERROR(@MESSAGE, @ERROR_SEVERITY, 1);
END CATCH;
然后打开实体Framework.EDMX文件,从数据库中更新并添加存储过程。没问题
然后我进入我的DAL,在这里写下这个方法
public List<VendorMaterials> GetMaterialsByVendorID(int id)
{
UsherEntities = new ExovationsUsherEntities();
List<VendorMaterials> lst = new List<VendorMaterials>();
var query = from a in UsherEntities.usp_GetMaterialByVendorID(id)
select a;
return lst;
}
就是抛出错误的地方,所以我在usp_GetMaterialByVendorID上做了一个F12,找到了它,看了看,它看起来和我的其他版本一样
public virtual int usp_GetMaterialByVendorID(Nullable<int> vendorID)
{
var vendorIDParameter = vendorID.HasValue ?
new ObjectParameter("VendorID", vendorID) :
new ObjectParameter("VendorID", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetMaterialByVendorID", vendorIDParameter);
}
正如编译器试图告诉您的那样,在int上运行LINQ查询是没有意义的。只需调用该函数。您在导入SP时是否选择Complex作为返回类型?@SLaks,我已经在DAL中编写了30多次相同类型的方法。那么,你能详细阐述一下你的评论吗?此存储过程根据传递给itI的id返回多条记录我刚刚进行了编辑并添加了一个几乎相同的方法,该方法使用相同的内容和works@IvanStoev,我所做的只是打开EDMX文件并右键单击它,然后从数据库中选择Update Model并添加新创建的存储过程。编译器试图告诉您,在int上运行LINQ查询是没有意义的。只需调用该函数。导入SP时是否选择Complex作为返回类型?@SLaks,我已经在DAL中编写了30多次相同类型的方法。那么,你能详细阐述一下你的评论吗?此存储过程根据传递给itI的id返回多条记录我刚刚进行了编辑并添加了一个几乎相同的方法,该方法使用相同的内容和works@IvanStoev,我所做的就是打开EDMX文件并在其中右键单击,然后选择Update Model from Database并添加新创建的存储过程
public virtual int usp_GetMaterialByVendorID(Nullable<int> vendorID)
{
var vendorIDParameter = vendorID.HasValue ?
new ObjectParameter("VendorID", vendorID) :
new ObjectParameter("VendorID", typeof(int));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetMaterialByVendorID", vendorIDParameter);
}
public List<VendorContact> GetVendorContactsByVendorID(int id)
{
UsherEntities = new ExovationsUsherEntities();
List<VendorContact> lst = new List<VendorContact>();
var query = from a in UsherEntities.usp_GetVendorContactByID(id)
select a;
foreach(var a in query)
{
lst.Add(new VendorContact
{
VendorID = a.VendorID,
ContactID = a.ContactID,
ContactName = a.ContactName,
FirstName = a.FirstName,
LastName = a.LastName,
Phone = a.Phone,
Cell = a.Cell,
Fax = a.Fax,
Email = a.Email
});
}
return lst;
}