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

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

当我在VS2015中使用ASP运行PM> Remove-Migration -context BloggingContext时。NET Core项目使用EF Core我得到以下错误:

System.InvalidOperationException: The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force) 
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.RemoveMigration(String contextType, Boolean force) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsRemoveCommand.<>c__DisplayClass0_0.<Configure>b__0() 
    at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args) 
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args) 
 The migration '20160703192724_MyFirstMigration' has already been applied to the database. Unapply it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.

如何取消申请?我使用的是ASP的最新版本。NET Core 1.0, EF Core和VS2015更新3。

我正在按照文档创建初始迁移。当我执行dotnet时,我得到了帮助部分,这意味着PATH正常工作。

然后我尝试在控制台窗口的文档中执行下面的命令:

dotnet ef迁移添加InitialCreate

我得到以下错误:

无法执行,因为没有找到指定的命令或文件。 可能的原因包括:

内置dotnet命令拼写错误。 您打算执行。net Core程序,但dotnet-ef不存在。 您打算运行一个全局工具,但是在PATH上找不到带有此名称的dotnet前缀的可执行文件。 由于复制了命令,所以排除了第一项。 我排除了第二项,因为安装了包Microsoft.EntityFrameworkCore.SqlServer。 我排除了第三项,因为在调用dotnet时会得到帮助部分。

我在谷歌上搜索这个问题,但因为这个版本是新的,所以没有太多可以继续的东西,而且/或者它淹没在早期版本的类似问题中。

我试图强行安装微软。EntityFrameworkCore,以防需要显式地添加它。我遇到了错误消息,告诉我可以选择的最新版本是2.2.6,降级是不行的。我不确定如何安装与系统上已有的sql包兼容的版本(更不确定这是否是解决此问题的正确方法)。

检测到包降级:Microsoft。EntityFrameworkCore从3.0.0-preview6.19304.10到2.2.6。直接从项目中引用包以选择不同的版本。 Web - > Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview6.19304.10 -> Microsoft.EntityFrameworkCore.Relational 3.0.0-preview6.19304.10 -> 微软。EntityFrameworkCore (>= 3.0.0-preview6.19304.10) 微软。EntityFrameworkCore (>= 2.2.6)