c# – 缺少Models / IdentityModel.cs

我是.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应用程序创建了一个新项目,其中包括基于个人用户帐户的身份验证.在教程中,我现在处于“创建您的模型”部分,并对如何继续感到困惑.

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

型号/ Model.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 templates而不是Visual C# Web Template shipped with Visual Studio 2015.

文件Models/IdentityModel.cs应包含ApplicationUser和ApplicationDbContext类.它们与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

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

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...