OrderBy throws无法加载文件或程序集System.Data.Entity

时间:2018-03-12 23:58:05

标签: c# asp.net-core entity-framework-core

尝试让DataTables在ASP.NET Core 2项目中工作。当执行以下代码行时,应用程序会在 OrderBy

上抛出异常
var data = (from app in _context.Applications select app);

//Sorting  
if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
{ 
    data = data.OrderBy(sortColumn + " " + sortColumnDir);
}

例外是:

{System.TypeInitializationException: The type initializer for 'System.Linq.Dynamic.ExpressionParser' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
   at System.Linq.Dynamic.ExpressionParser..cctor()
   --- End of inner exception stack trace ---
   at System.Linq.Dynamic.ExpressionParser..ctor(ParameterExpression[] parameters, String expression, Object[] values)
   at System.Linq.Dynamic.DynamicQueryable.OrderBy(IQueryable source, String ordering, Object[] values)
   at System.Linq.Dynamic.DynamicQueryable.OrderBy[T](IQueryable`1 source, String ordering, Object[] values)
   at assetreportv2.Controllers.HomeController.LoadData() in C:\Users\chentiangemalc\Documents\Visual Studio 2017\Projects\DataTableTest\DataTableTest\Controllers\HomeController.cs:line 64}

csproj文件目前配置如下:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="bootstrap" Version="4.0.0" />
    <PackageReference Include="Entityframework" Version="6.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.2" />
    <PackageReference Include="System.Linq.Dynamic" Version="1.0.7" />
    <PackageReference Include="WindowsAzure.Storage" Version="8.3.0" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.1" />
  </ItemGroup>

  <ItemGroup>
    <WCFMetadata Include="Connected Services" />
  </ItemGroup>

</Project>

有没有办法在ASP.NET Core中使用此OrderBy,或建议其他选项?

1 个答案:

答案 0 :(得分:4)

抱歉,我的问题有点迟了,但我确实遇到了这个问题而且我刚修好了。我也使用Core 2.0并使用System.Linq.Dynamic并在使用OrderBy扩展程序时出现错误,但我卸载了它并安装了System.Linq.Dynamic.Core,现在它正常运行