我在我的實體框架DbContext中有一個方法List<EmployeeView> EmployeeFilter(string myFilter)
。它永遠不會結束。當執行結果,在result.RawView.Message是SqlParameter已包含在另一個SqlParameterCollection(EF,SqlQuery,存儲過程調用)
的SqlParameter已經被其他SqlParameterCollection
public List<EmployeeView> EmployeeFilter(string myFilter)
{
if (myFilter == null)
myFilter = "";
SqlParameter parameter = new SqlParameter
{
ParameterName = "@filter",
IsNullable = true,
Direction = ParameterDirection.Input,
DbType = DbType.String,
Size = 1000,
Value = myFilter
};
var result = Database.SqlQuery<EmployeeView>("exec EmployeeFilter @filter", parameter);
List<EmployeeView> employeeList = result.ToList();
return employeeList;
}
我試圖重寫方法如下遏制。但沒有任何幫助的
var result = Database.SqlQuery<EmployeeView>("exec EmployeeFilter @filter", parameter);
List<EmployeeView> employeeList = result.ToList();
2.
var parameters = new SqlParameter[] { new SqlParameter("@filter", SqlDbType.NVarChar, 1000) { Value = myFilter} };
或
SqlParameter parameter = new SqlParameter("filter", SqlDbType.NVarChar, 1000);
parameter.Value = myFilter;
1.
var result = Database.SqlQuery<EmployeeView>("exec EmployeeFilter @filter", parameter).ToList();
代替或
SqlParameter parameter = new SqlParameter("@filter", SqlDbType.NVarChar, 1000);
parameter.Value = myFilter;
或
SqlParameter parameter = new SqlParameter("@filter", myFilter);
存儲過程的代碼:
procedure [dbo].[EmployeeFilter]
(@filter nvarchar(1000))
as
declare @countFilter as int
if object_id('tempdb.dbo.#tempFilterValues') is not NULL
drop table #tempFilterValues
select display_term
into #tempFilterValues
from sys.dm_fts_parser(@filter, 1049, 0, 0)
select @countFilter = count(*)
from #tempFilterValues
if @countFilter = 0
--если фильтра нет, то незачем усложнять селект группировкой и поиском
select e.Id, e.DateOfBirth, e.IdentityCodeChar, e.FirstName, e.SecondName, e.Surname, e.Age,
e.ShortAddress, e.Position
from dbo.EmployeeView e
else
begin
--если в фильтре одна запись
--появился поиск по like
if @countfilter = 1
select e.Id, e.DateOfBirth, e.IdentityCodeChar, e.FirstName, e.SecondName, e.Surname, e.Age,
e.ShortAddress, e.Position
from dbo.EmployeeView e
inner join #tempFilterValues as ParseString
on e.Position + e.FirstName + e.SecondName + e.Surname like '%'+ ParseString.display_term +'%'
else
--если несколько значений в фильтре
--like
--появляется группировка
select e.Id, e.DateOfBirth, e.IdentityCodeChar, e.FirstName, e.SecondName, e.Surname, e.Age,
e.ShortAddress, e.Position
from dbo.EmployeeView e
inner join #tempFilterValues as ParseString
on eo.Position+p.FirstName + p.SecondName + p.Surname like '%'+ ParseString.display_term +'%'
group by e.Id, e.DateOfBirth, e.IdentityCodeChar, e.FirstName, e.SecondName, e.Surname, e.Age,
e.ShortAddress, e.Position
having count(e.Id) > 1
end
異常逮住:
error "System.Data.Entity.Core.EntityCommandExecutionException: The data reader is incompatible with the specified 'GTIApp.Models.EmployeeView'. A member of the type, 'AgeCount', does not have a corresponding column in the data reader with the same name.\r\n в System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetMemberOrdinalFromReader(DbDataReader storeDataReader, EdmMember member, EdmType currentType, Dictionary`2 renameList)\r\n в System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary`2 renameList)\r\n в System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory.CreateColumnMapFromReaderAndType(DbDataReader storeDataReader, EdmType edmType, EntitySet entitySet, Dictionary`2 renameList)\r\n в System.Data.Entity.Core.Objects.ObjectContext.InternalTranslate[TElement](DbDataReader reader, String entitySetName, MergeOption mergeOption, Boolean streaming, EntitySet& entitySet, TypeUsage& edmType)\r\n в System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryInternal[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)\r\n в System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__64()\r\n в System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)\r\n в System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass65`1.<ExecuteStoreQueryReliably>b__63()\r\n в System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n в System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQueryReliably[TElement](String commandText, String entitySetName, ExecutionOptions executionOptions, Object[] parameters)\r\n в System.Data.Entity.Core.Objects.ObjectContext.ExecuteStoreQuery[TElement](String commandText, ExecutionOptions executionOptions, Object[] parameters)\r\n в System.Data.Entity.Internal.InternalContext.<>c__DisplayClass14`1.<ExecuteSqlQuery>b__13()\r\n в System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\n в System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n в System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n в GTIApp.Models.DbPersonnel.EmployeeFilter(String myFilter)" string
請幫助!
後的*全*例外,包括調用堆棧相匹配。這將顯示異常實際發生的位置。使用'Exception.ToString()' –
一切都很簡單...類EmployeeView具有屬性int? AgeCount,我的程序有欄目年齡,而不是AgeCount ...謝謝! – sophie