简体   繁体   English

使用实体框架和标识分离 ASP.NET MVC 中的模型

[英]Separating Models in ASP.NET MVC with Entity Framework and Identity

Using Visual Studio 2017. I am following a tutorial (based on a previous version of VS if that makes any difference) which has created a standard ASP.NET MVC project with Identity.使用 Visual Studio 2017。我正在学习一个教程(基于以前版本的 VS,如果有什么不同),它创建了一个带有 Identity 的标准 ASP.NET MVC 项目。 As I understand it, it installs Entity Framework as well by default.据我了解,默认情况下它也会安装实体框架。

I now want to separate the models into a separate class Library.我现在想将模型分离到一个单独的类库中。 The Models folder currently represents the standard models that come when creating a new ASP.NET MVC "out of the box" project. Models文件夹当前代表创建新的 ASP.NET MVC“开箱即用”项目时出现的标准模型。 So I add new empty CL project and move all the Model classes as shown here:所以我添加新的空 CL 项目并移动所有Model类,如下所示:

在此处输入图片说明

I add two additional classes as I would like to use the existing models and the new classes would represent new tables within my database.我添加了两个额外的类,因为我想使用现有模型,新类将代表我的数据库中的新表。

At the moment I get compile errors about references not being available ie目前,我收到有关引用不可用的编译错误,即

The type or namespace name 'AspNet' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)命名空间“Microsoft”中不存在类型或命名空间名称“AspNet”(您是否缺少程序集引用?)

The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)命名空间“System.Web”中不存在类型或命名空间名称“Mvc”(您是否缺少程序集引用?)

Looked at my assemblies and they are all empty which eventually led me to看着我的程序集,它们都是空的,最终导致我

Visual Studio 2017 won't load .NET Framework references in .NET Standard library Visual Studio 2017 不会在 .NET Standard 库中加载 .NET Framework 引用

  1. This makes me wonder if I may have done this wrong or missed a step somewhere;这让我怀疑我是否做错了这件事或在某处错过了一步; how do I separate the model classes from an ASP.NET MVC project and put them into a new class library, so this class can be used against other projects?如何将模型类与 ASP.NET MVC 项目分开并将它们放入新的类库中,以便此类可以用于其他项目?

  2. I would have 3 projects in total, Models (all class/database table models), Business Layer (where I would do CRUD operations) - would I install Entity Framework 6.4 into this class project?我总共有 3 个项目, Models (所有类/数据库表模型), Business Layer (我将在其中执行 CRUD 操作)-我是否将 Entity Framework 6.4 安装到此类项目中? Then reference these two projects within the main ASP.NET MVC project?然后在主 ASP.NET MVC 项目中引用这两个项目?

You need install the following nuget packages in your class library:您需要在类库中安装以下 nuget 包:

  1. Microsoft.AspNet.Identity.EntityFramework Microsoft.AspNet.Identity.EntityFramework
  2. Microsoft.AspNet.Identity Microsoft.AspNet.Identity
  3. Microsoft.AspNet.Mvc微软.AspNet.Mvc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM