'DbContextOptionsBuilder'不包含'UseSqlServer'的定义

编程入门 行业动态 更新时间:2024-10-20 07:51:32
本文介绍了'DbContextOptionsBuilder'不包含'UseSqlServer'的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用C#和目标.NET Core在VS 2015 Pro(Update 3)中创建Web API.

I am trying to create a Web API in VS 2015 Pro (Update 3) using C# and targeting .NET Core.

我正在遵循本教程: docs.efproject/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework . 但是,我正在连接到MySQL数据库而不是SQL Server数据库-不知道有什么区别...

I am following this tutorial: docs.efproject/en/latest/platforms/aspnetcore/new-db.html#install-entity-framework. However, I am connecting to a MySQL database instead of an SQL Server database - not sure how much difference that makes...

无论如何,在本教程中,我必须通过依赖项注入注册我的上下文" -因此,我必须将以下行添加到 Startup.cs 文件:

Anyway, in the tutorial, I have to "Register my context with dependency injection" - so I have to add the following line to the ConfigureServices section of the Startup.cs file:

var connection = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection));;

但是,VS给我以下错误:

However, VS gives me the following error:

错误CS1061'DbContextOptionsBuilder'不包含'UseSqlServer'的定义,并且找不到扩展方法'UseSqlServer'接受类型为'DbContextOptionsBuilder'的第一个参数(是否缺少using指令或程序集引用?)

有什么想法吗?

这是整个Startup.cs文件的样子:

This is what the whole Startup.cs file looks like:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace PropWorxAPI { public class Startup { public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } // 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(); var connection = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext< Models.PropWorxContext > (options => options.UseSqlServer(connection)); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseMvc(); } } }

我还使用软件包管理器添加了MySQL软件包,以便我的 project.json 文件包含以下条目:

I have also added the MySQL package using the Package Manager, so that my project.json file contains this entry:

*"MySql.Data.EntityFrameworkCore": "7.0.6-IR31"*

关于我哪里出了问题的任何提示将不胜感激,因为我花了一整天的时间来弄清楚它:(谢谢...

Any hints as to where I've gone wrong would be greatly appreciated, as I've spent all day trying to figure it out :( Thank you...

推荐答案

SqlServer是Microsoft Sql Server,而不是MySql,要使用SqlServer,您需要包"Microsoft.EntityFrameworkCore.SqlServer":"1.0.*".

SqlServer is Microsoft Sql Server not MySql, to use SqlServer you would need the package "Microsoft.EntityFrameworkCore.SqlServer": "1.0.*".

如果使用MySql,则有选项.UseMySql

If using MySql there is options.UseMySql

请参见更多教程

更多推荐

'DbContextOptionsBuilder'不包含'UseSqlServer'的定义

本文发布于:2023-11-08 13:53:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包含   定义   DbContextOptionsBuilder   UseSqlServer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!