gpt4 book ai didi

asp.net-core - Entity Framework 核心: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver'

转载 作者:行者123 更新时间:2023-12-03 05:06:11 32 4
gpt4 key购买 nike

我是 EF core 的新手,我正在尝试让它与我的 ASP.NET Core 项目配合使用。

尝试将 DbContext 配置为使用配置中的连接字符串时,我在 startup.cs 中收到上述错误。我正在关注this tutorial .

有问题的代码位于 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 )。

最佳答案

首先我们安装Microsoft.EntityFrameworkCore.SqlServer NuGet 包:

PM > Install-Package Microsoft.EntityFrameworkCore.SqlServer

然后,在导入命名空间后

using Microsoft.EntityFrameworkCore;

我们添加数据库上下文:

services.AddDbContext<AspDbContext>(options =>
options.UseSqlServer(config.GetConnectionString("optimumDB")));

关于asp.net-core - Entity Framework 核心: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098065/

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