实体框架核心:DbContextOptionsBuilder不包含“usesqlserver”的定义 也没有扩展方法“usesq

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

我是 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");
        }

我所有的在线研究都指向缺少的参考文献,但我似乎无法找出我缺少的是哪一个(see image)。

707

首先,我们安装Microsoft.EntityFrameworkCore.SqlServerNuGet 包:

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

然后,在导入命名空间后,使用

using Microsoft.EntityFrameworkCore;

我们添加数据库上下文:

services.AddDbContext<AspDbContext>(options =>
    options.UseSqlServer(config.GetConnectionString("optimumDB")));
138
adding using Microsoft.EntityFrameworkCore;

手动解决了我的问题

Found that here Edit...

对于 dotnet 核心 3.1 添加

Microsoft.EntityFrameworkCore.SqlServer
37

请按照以下步骤操作。

安装实体框架核心设计和实体框架核心的 SQL Server 数据库提供程序:

dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

导入实体框架核心:

using Microsoft.EntityFrameworkCore;

并配置您的 DbContext:

var connectionString = Configuration.GetConnectionString("myDb");
services.AddDbContext<MyDbContext>(options => 
    options.UseSqlServer(connectionString)
);
36

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

Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.SqlServer

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(93)
.NetBlazor优于Angular React或其他javascript框架
上一篇
使用CSS我可以检查浏览器是否支持“CSS属性和值API”(Houdini )@ property规则
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(17条)