gpt4 book ai didi

entity-framework-core - NU1107 : Version conflict detected for Microsoft. EntityFrameworkCore.Abstractions。安装 EFCore 以与 AspNetCore 2.2 一起使用

转载 作者:行者123 更新时间:2023-12-05 05:02:40 28 4
gpt4 key购买 nike

我能够升级我的 .NetStandard2.0 项目以使用 EntityFrameworkCore 3.1.4 并创建一个 nuget 包。

现在我想在我的 netcoreapp2.2 项目中使用那个 nuget 包。

但是,当我尝试通过 nuget 添加包时,我得到了

Error   NU1107  Version conflict detected for Microsoft.EntityFrameworkCore. Install/reference Microsoft.EntityFrameworkCore 3.1.4 directly to project MyApi to resolve this issue. 
MyApi -> SBDSTD.Standard 1.0.0-CI-20200526-230146 -> Microsoft.EntityFrameworkCore (>= 3.1.4)
MyApi -> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.EntityFrameworkCore (>= 2.2.0 && < 2.3.0). MyApi D:\dev\MyApi\MyApi\MyApi.csproj 1

所以我尝试使用安装 Entity Framework 包

Install-Package Microsoft.EntityFrameworkCore -Version 3.1.4

我得到了

NU1608: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.2.0 requires Microsoft.EntityFrameworkCore (>= 2.2.0 && < 2.3.0) but version Microsoft.EntityFrameworkCore 3.1.4 was resolved.
Install-Package : NU1107: Version conflict detected for Microsoft.EntityFrameworkCore.Abstractions. Install/reference Microsoft.EntityFrameworkCore.Abstractions 3.1.4 directly to project
MyApito resolve this issue.
MyApi-> Microsoft.EntityFrameworkCore 3.1.4 -> Microsoft.EntityFrameworkCore.Abstractions (>= 3.1.4)
MyApi-> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.EntityFrameworkCore.Abstractions (>= 2.2.0 && < 2.3.0).
At line:1 char:1
+ Install-Package Microsoft.EntityFrameworkCore -Version 3.1.4
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Install-Package], Exception
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

我不愿意开始尝试安装建议的包,因为我刚刚花了半天时间在包管理器 UI 中做类似的事情[更新]当我开始通过 Nuget 安装时,我看到一条消息

Detected package version outside of dependency constraint:Microsoft.EntityFrameworkCore.Abstractions (>=2.2.0 && < 2.3.0) butversion Microsoft.EntityFrameworkCore.Abstractions 3.1.5 was resolved

如果我将我的库回滚到使用 EntityFrameworkCore 2.2.0 并且必须将所有 Microsoft.Data.SqlClient 更改为 System.Data.SqlClient 引用。

[更新]按照 Lauxjpn 的建议并粘贴我得到的引用资料

Detected Package version error

我开始尝试解决错误。我得到的第一个错误是在

using Remotion.Linq.Clauses;

我只是简单地注释掉了。

然后我就到了

this conversion error

CS0619  'RelationalQueryableExtensions.FromSql<TEntity>(IQueryable<TEntity>, RawSqlString, params object[])' is obsolete: 'For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query

最佳答案

根本问题是,Microsoft.AspNetCore.App 元包引用了 EF Core(和其他)包的特定版本范围。

[...] Install/reference Microsoft.EntityFrameworkCore 3.1.4 directly to project MyApi to resolve this issue.

一个简单的解决方案是执行消息告诉您的操作:将要覆盖的包版本显式添加到目标项目。

假设一个简单的原始项目文件如下:

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

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

</Project>

然后以下将使您的项目编译:

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

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<!-- Manually override all conflicting packages we care about and their
dependencies, that are referenced by `Microsoft.AspNetCore.App`. -->
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4"/>

<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.1.4" />
</ItemGroup>

</Project>

但是,这会导致一些警告。如果您不想要这些,则必须引用各个包,Microsoft.AspNetCore.App 元包可以方便地为您组合,并适当更新它们的版本:

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

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<!-- Don't use the meta package anymore. We will explicitly reference
all actual packages instead.-->
<!--<PackageReference Include="Microsoft.AspNetCore.App" />-->
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<!-- Manually include all packages from "Microsoft.AspNetCore.App",
and update all package versions unrelated to ASP.NET Core. -->
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.6"/>
<PackageReference Include="Microsoft.AspNetCore.Antiforgery" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OAuth" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Twitter" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.WsFederation" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization.Policy" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Cryptography.Internal" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Extensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.DataProtection" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.HostFiltering" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Server.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Connections.Common" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Connections" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.HttpOverrides" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Localization.Routing" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Localization" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.MiddlewareAnalysis" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Analyzers" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Cors" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Xml" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Localization" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.TagHelpers" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.NodeServices" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Owin" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Razor.Runtime" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.ResponseCaching.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.ResponseCaching" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.HttpSys" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.IIS" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Core" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Https" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Abstractions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.Json" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0"/>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Razor" Version="2.2.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.KeyPerFile" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Composite" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.2.0"/>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Localization" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.EventSource" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.1.4"/>
<PackageReference Include="Microsoft.Extensions.WebEncoders" Version="3.1.4"/>
<PackageReference Include="Microsoft.Net.Http.Headers" Version="2.2.0"/>
<PackageReference Include="System.IO.Pipelines" Version="4.5.2"/>
</ItemGroup>

</Project>

更新:

对于引用问题解决后现在出现的问题,这些现在是与您从 EF Core 2.2 升级相关的问题。到 3.1.4。

(从技术上讲,这现在是一个完全不同的问题,答案完全不同,并且与您原来的问题无关。因此,我建议下次就 SO 提出一个全新的问题。)

有很多重要的breaking changes在 EF Core 3.0 中,因为查询管道被完全重写。

其中一个变化是,EF Core 不再依赖于 Remotion

至于您的 ExecuteSqlQuery() 扩展方法,以下实现应该有效:

public static class DatabaseFacadeExtensions
{
public static RelationalDataReader ExecuteSqlQuery(
this DatabaseFacade databaseFacade,
string sql,
params object[] parameters)
{
var concurrencyDetector = databaseFacade.GetService<IConcurrencyDetector>();
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade
.GetService<IRawSqlCommandBuilder>()
.Build(sql, parameters);

var connection = databaseFacade.GetService<IRelationalConnection>();
var parameterObj = new RelationalCommandParameterObject(
connection,
rawSqlCommand.ParameterValues,
null,
null,
null);
return rawSqlCommand.RelationalCommand.ExecuteReader(parameterObj);
}
}
}

CS0619 'RelationalQueryableExtensions.FromSql(IQueryable, RawSqlString, params object[])' is obsolete: 'For returning objects from SQL queries using plain strings, use FromSqlRaw instead. For returning objects from SQL queries using interpolated string syntax to create parameters, use FromSqlInterpolated instead. Call either new method directly on the DbSet at the root of the query

对于上面的警告,它已经告诉您该怎么做(请改用 FromSqlRawFromSqlInterpolated)。这也是 EF Core 3.0 的变化。参见 Raw SQL Queries了解更多信息。

这是一个完全可用的控制台示例,它演示了两种功能(您的 ExecuteSqlQuery() 扩展方法和 FromSqlInterpolated()):

using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;

namespace IssueConsoleTemplate
{
public class IceCream
{
public int IceCreamId { get; set; }
public string Name { get; set; }
}

public static class DatabaseFacadeExtensions
{
public static RelationalDataReader ExecuteSqlQuery(
this DatabaseFacade databaseFacade,
string sql,
params object[] parameters)
{
var concurrencyDetector = databaseFacade.GetService<IConcurrencyDetector>();
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade
.GetService<IRawSqlCommandBuilder>()
.Build(sql, parameters);

var connection = databaseFacade.GetService<IRelationalConnection>();
var parameterObj = new RelationalCommandParameterObject(
connection,
rawSqlCommand.ParameterValues,
null,
null,
null);
return rawSqlCommand.RelationalCommand.ExecuteReader(parameterObj);
}
}
}

public class Context : DbContext
{
public DbSet<IceCream> IceCreams { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(@"Data Source=.\MSSQL14;Integrated Security=SSPI;Initial Catalog=So63009531")
.UseLoggerFactory(
LoggerFactory.Create(
b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IceCream>()
.HasData(
new IceCream {IceCreamId = 1, Name = "Vanilla"},
new IceCream {IceCreamId = 2, Name = "Chocolate"});
}
}

internal static class Program
{
private static void Main()
{
using var context = new Context();

context.Database.EnsureDeleted();
context.Database.EnsureCreated();

var iceCreams = context.IceCreams
.OrderBy(u => u.IceCreamId)
.ToList();

var vanillaName = "Vanilla";
var vanillaIceCreamCountReader = context.Database.ExecuteSqlQuery(
@"select count(*) as [IceCreamCount]
from [dbo].[IceCreams]
where Name = @p0;",
vanillaName);
vanillaIceCreamCountReader.Read();
var vanillaIceCreamCount = (int)vanillaIceCreamCountReader.DbDataReader["IceCreamCount"];
vanillaIceCreamCountReader.Dispose();

var chocolateName = "Chocolate";
var chocolateIceCreams = context.IceCreams
.FromSqlInterpolated($"select * from [dbo].[IceCreams] where [Name] = {chocolateName}")
.ToList();

Debug.Assert(iceCreams.Count == 2);
Debug.Assert(vanillaIceCreamCount == 1);
Debug.Assert(chocolateIceCreams.Count == 1);
}
}
}

请注意,由于 EF Core 3.0 中的查询管道发生了重大更改,您的查询可能会与以前不同(关键字为 cartesian explosion)。

关于entity-framework-core - NU1107 : Version conflict detected for Microsoft. EntityFrameworkCore.Abstractions。安装 EFCore 以与 AspNetCore 2.2 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62034467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com