c# - 缺少模型/IdentityModel.cs

标签 c# asp.net-core asp.net-core-mvc

我是 .NET 的新手,目前正在通过关注 this tutorial 学习 ASP.NET Core MVC .

我的开发环境:

  • Ubuntu 16.04.1 LTS
  • .Net Core 1.0.0-preview2-1-003177
  • Visual Studio Code 1.7.2
  • generator-aspnet 0.2.5(创建项目脚手架)

因此,我使用 yo aspnet 创建了一个新项目并选择了 Web Application,其中包括基于个人用户帐户的身份验证。在本教程中,我现在处于“创建模型”部分,对如何继续感到困惑。

具体来说,本教程要求您创建的模型如下所示:

模型/模型.cs:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
}

但是,在那个页面上,有一个警告:

If you use Individual User Accounts instead of None for Authentication then an Entity Framework model will be added to your project in Models\IdentityModel.cs. Using the techniques you will learn in this walkthrough, you can choose to add a second model, or extend this existing model to contain your entity classes.

当我找不到他们引用的文件 Models/IdentityModel.cs 时,我开始感到困惑。我确实看到了 Models/ApplicationUser.cs,它看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
    }
}

...和Data/ApplicationDBContext.cs,它看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using EFGetStarted.AspNetCore.NewDb.Models;
namespace EFGetStarted.AspNetCore.NewDb.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
}

上面的警告消息让我对如何继续感到困惑。由于 Models/IdentityModel.cs 不存在,我是将 Model.cs 的内容放入 Models/ApplicationUser.cs 或 Data/ApplicationDBContext.cs 还是保留所有内容?

最佳答案

I could not find the file they reference, Models/IdentityModel.cs.

generator-aspnet 正在使用 different version of templatesVisual C# Web Template shipped with Visual Studio 2015 .

文件 Models/IdentityModel.cs应包含类 ApplicationUserApplicationDbContext。它们与 generator-aspnet 使用的版本相同。

do I place the contents of Model.cs into Models/ApplicationUser.cs or Data/ApplicationDBContext.cs or do I leave everything as is

您可以保持原样 - 这些文件位于何处并不重要。

关于c# - 缺少模型/IdentityModel.cs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41003750/

相关文章:

c# - 避免有细微差别的重复循环

c# - 如何模拟仅在运行时持续存在的内存中的数据库?

c# - 如何强制我的 .NET 应用程序以管理员身份运行?

asp.net-core - 加载 aspnetcore RC2 默认网站项目时出现“错误网关”错误

javascript - 获取外键表数据

c# - 如何在 View 之外使用 ASP.NET Core 生成完整的 URL?

asp.net-core - MVC 6 .NET Core 与 .NET Framework 4.6 性能比较

c# - 如何在 ASP.NET Core MVC 中获取文件的最后修改日期?

C# - 如何循环遍历表以更新每一行 MySQL

C# : Change String Encoding?