fir*_*ter 5 c# asp.net-core-mvc asp.net-core
我是.NET的新手,目前正在通过阅读本教程来学习ASP.NET Core MVC 。
我的开发环境:
因此,我使用yo aspnet并选择了一个新项目Web Application,其中包括基于个人用户帐户的身份验证。在本教程中,我现在在“创建模型”部分,对如何继续感到困惑。
具体来说,本教程要求您创建的模型如下所示:
型号/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; }
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在该页面上有一个警告:
如果您使用“个人用户帐户”而不是“无”进行身份验证,则实体框架模型将添加到您的项目的Models \ IdentityModel.cs中。使用您将在本演练中学习的技术,可以选择添加第二个模型,或扩展此现有模型以包含您的实体类。
当我找不到它们引用的文件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
{
}
}
Run Code Online (Sandbox Code Playgroud)
...和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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的警告消息使我对如何继续感到困惑。由于不存在Models / IdentityModel.cs,我应该将Model.cs的内容放入Models / ApplicationUser.cs或Data / ApplicationDBContext.cs中还是将所有内容都保留不变?
Mar*_*elo -1
当您选择“无身份验证”以外的身份验证方法时,会出现此警告。
如果您选择“个人用户帐户”,将为使用数据库创建一个新的实体框架模型。
您可能选择了“无身份验证”,这就是为什么您没有“ Models/IdentityModel.cs”文件
| 归档时间: |
|
| 查看次数: |
4032 次 |
| 最近记录: |