数据库第一错误实体框架.NET核心

时间:2016-08-10 11:45:01

标签: c# asp.net-core entity-framework-core

我正在将旧的类库转换为类库包。这个类库是旧项目的DAL层 - 我们将升级到最新的.NET。

在旧的类库中,我们首先使用edmx文件作为数据库。在新的.NET中,对edmx的支持已经停止,但我们仍然需要从数据库生成模型 - 因为数据库已经到位。

为此,我按照此链接中提供的步骤进行操作:https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

所以我所做的就是创建一个新的类库包:

enter image description here

project.json是这样的:

{
  "version": "1.0.0-*",
  "description": "xPT.DAL Class Library",
  "authors": [ "Dawood" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",
  "frameworks": {
    "net46": {
      "dependencies": {
        "Microsoft.CSharp": "4.0.1",
        "System.Collections": "4.0.11",
        "System.Linq": "4.1.0",
        "System.Runtime": "4.1.0",
        "System.Threading": "4.0.11"
      }
    }
  },
  "dependencies": {

  }
}

现在,当我安装:Microsoft.EntityFrameworkCore.SqlServer时,如上面的链接中所述,我收到此错误:

使用此命令:Install-Package Microsoft.EntityFrameworkCore.SqlServer

enter image description here

错误列表:

enter image description here

它在这里说:

The dependency Microsoft.Extensions.Caching.Abstractions 1.0.0 in project xPT.DAL does not support framework .NETFramework,Version=v4.6

但根据文档,上述所有4.5.1应该支持此库:https://docs.efproject.net/en/latest/providers/sql-server/index.html#supported-platforms

我做错了什么?

3 个答案:

答案 0 :(得分:3)

<强>更新
看起来你还在使用DNX。请升级到.NET Core(以及.NET CLI),因为不再支持DNX。您可以下载.NET Core here

net46是桌面.NET框架4.6的Target Framework Moniker(TMF)。您不需要在该框架上引用Microsoft.CSharpSystem.Collections等软件包,因为默认情况下它们是完整.NET框架的一部分。您只需要定位.NET标准版本,例如netstandard1.6

删除您在net46下列出的所有违规行为:

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0"
  },

  "frameworks": {
    "net46": {}
  }
}

答案 1 :(得分:2)

好的,我花了大约一天的时间来弄清楚,所以我在这里发布了我的数据库首先使用.NET Core Web App在Class Project (.NET Core)中工作的步骤。

第1步 - 安装.NET Core

确保您使用的是.NET Core而非DNX (Hint: You should be able to see the .NET Core option when creating a New Project) - 如果不是从Here下载

如果您在安装.NET Core时出现问题(错误类似于未正确安装Visual Studio 2015 Update 3) - 您可以使用以下命令运行安装:[DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1] - 这将阻止安装执行Visual Studio检查Github Issue

enter image description here

第2步 - 创建项目

创建新的ASP.NET核心Web应用程序 - &gt;然后在下一个屏幕中选择Web应用程序

enter image description here

添加Class Library (.NET Core)项目

enter image description here

第3步 - 安装EF包

打开类库的project.json文件,然后粘贴以下内容,然后保存文件:

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "NETStandard.Library": "1.6.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "net46": {
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    }
  }
}

这应该恢复References

下的包

enter image description here

----------------或

您可以使用Nuget Package Manager通过在程序包管理器控制台中运行以下命令来安装它们

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.Tools –Pre

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

注意:一次安装一个包 - 如果安装后出现错误

Microsoft.EntityFrameworkCore.Tools

然后将project.json框架部分的内容更改为:

  "frameworks": {
    "net46": {
    },
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    }
  }

第4步 - 创建数据库模型

现在要生成数据库,请在Package Manager Console中运行以下命令(不要忘记将连接字符串更改为数据库)

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer

这将为您提供有关启动项目的错误:

enter image description here

为此,您必须将添加到类库中的相同引用添加到.NET Web App

为Web App打开project.json

dependencies下,添加:

"Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

并在tools下添加:

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

进行更改后保存文件。

这就是我的project.json看起来像

enter image description here

然后再次在Package Manager Console中针对类库运行命令:

如果您尚未将类库的引用添加到Web应用程序,则会出现此错误:

enter image description here

解决此问题,将类库的引用添加到Web App:

enter image description here

最后

再次运行命令 - 在Package Manager Console

Scaffold-DbContext "Server=. ; Database=DATABASE; user id= USER ; password = PASSWORD;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

这应该在类库

中的Models文件夹下创建实体

enter image description here

答案 2 :(得分:1)

!!!!停止使用DNX !!!!

ASP.NET Core 1.0.0 did already hit RTM 1 1/2个月前。

自RC2以来,DNX不受支持且未得到维护

您必须使用RC1或更早版本,因为它是最后一次DNX版本。

RTM版本的软件包无法与RTM配合使用!卸载(ASP).NET 5工具并从Microsoft页面安装.NET Core SDK