我是新的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中的良好社区

其他回答

目前正在使用实体框架核心3.1.3。以上的解决方案都不能解决我的问题。

但是,在我的项目上安装包microsoft . entityframeworkcore .代理解决了这个问题。现在我可以访问uselazyloadingagents()方法调用时设置我的DBContext选项。

希望这能帮助到一些人。请看下面的文章:

EF核心的惰性加载

包裹不见了。打开包管理器控制台,执行下面的代码:

Install-Package 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")));
        }

对于仍然有这个问题的人: 使用NuGet安装: Microsoft.EntityFrameworkCore.Proxies

这个问题与使用EFCore的Castle Proxy有关。

我有这个问题,似乎我没有添加所需的NuGet包,虽然我认为我已经这样做了,一定要检查他们,一个接一个。