我是新的EF核心,我试图让它与我的ASP工作。NET Core项目。

当我尝试配置DbContext使用config中的连接字符串时,我在startup.cs中得到了上述错误。我正在遵循这个教程。

有问题的代码在startup.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.Webpack;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using tracV2.models;
using tracV2.data;

namespace tracV2
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();

            services.AddSingleton<IConfiguration>(Configuration);

            string conn = Configuration.GetConnectionString("optimumDB");

            services.AddDbContext<tracContext>(options => options.usesqlserver(conn));
        }

如果我把UseSqlServer方法直接放到上下文中,它就会被识别出来:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace tracV2.data
{
    public class tracContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("myrealconnectionstring");
        }

我在网上的所有研究都指向了缺失的参考文献,但我似乎找不到我缺失的是哪一本(见图)。


当前回答

你的解决方案很有效。

当我看到这个视频直到17分钟:https://www.youtube.com/watch?v=fom80TujpYQ 我遇到了一个问题:

 services.AddDbContext<PaymentDetailContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));

UseSqlServer不能识别,所以我这样做了 安装- package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.5

& 使用Microsoft.EntityFrameworkCore;

那我的问题就解决了。关于我:基本上我从一开始就是一个纯粹的PHP程序员,今天只有我开始了。net编码,感谢在。net中的良好社区

其他回答

安装下面的NuGet包将解决您的问题

Microsoft.EntityFrameworkCore.SqlServer

安装包Microsoft.EntityFrameworkCore.SqlServer

首先添加Install-Package Microsoft.EntityFrameworkCore.SqlServer

接下来使用Microsoft.EntityFrameworkCore添加到你的.cs文件中;

最后将其添加到你的核心Startup.cs中

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MovieContext")));
        }

正如Win的高分答案所提到的,你可能需要安装Microsoft.EntityFrameworkCore.SqlServer NuGet包,但请注意,这个问题使用的是asp.net core mvc。在最新的ASP。在NET Core 2.1中,微软包含了一个叫做微软. aspnetcore . app的元包

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.2

如果右键单击ASP. xml,就可以看到对它的引用。在解决方案资源管理器中选择“编辑项目文件”

如果ASP。NET core webapps using语句

<包引用包括=“Microsoft.AspNetCore.App” />

Microsoft.EntityFrameworkCore.SqlServer包含在这个元包中。所以在你的Startup.cs中,你只需要添加:

使用Microsoft.EntityFrameworkCore;

当我转移到Microsoft.EntityFrameworkCore.SqlServer v3.0.0和Microsoft.EntityFrameworkCore.Tools v3.0.0时,我遇到了这个问题

当我在两个库上都改回v2.2.6时,错误就消失了。这更像是一种变通方法而不是解决方案,但它会让你启动并运行,直到问题得到解决。

我添加SqlServer和Sqlite到我的项目,同样的问题出现了。

对我来说,我安装了微软。EntityFrameworkCore之前的版本是5.0.6。现在Microsoft.EntityFrameworkCore.SqlServer的版本是5.0.7。安装后没有UserSqlServer方法。

1. 请尝试升级工具版本以保持一致

尝试在csproj中修改版本:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.7">

或者打开NuGet来更新包。

2. 重新启动Visual Studio

如果它不能帮助你,最重要的是重新启动VS

然后,一切都好了。

参考 https://github.com/dotnet/efcore/issues/7891