[英]ASP.NET Core Web Application: create model class that has default User model as property
I have created a new project using the template for "ASP.NET Core 3.0 Web Application (Model View Controller)".我使用“ASP.NET Core 3.0 Web 应用程序(模型视图控制器)”的模板创建了一个新项目。 It comes with authentication (register, login, logout, etc).它带有身份验证(注册、登录、注销等)。
I now want to create a model class called InsurancePolicy that has the User property (or UserId property), obviously referencing a user in the default AspNetUsers table in my localdb.我现在想创建一个名为 InsurancePolicy 的 model class,它具有 User 属性(或 UserId 属性),显然在我的 localdb 中的默认 AspNetUsers 表中引用了一个用户。
So EFCore is code first for modelling I understand, so I have created that InsurancePolicy model class, with all the properties like Description etc. I'm following this guideline by Microsoft.所以 EFCore 是我理解的建模代码优先,所以我创建了 InsurancePolicy model class,具有描述等所有属性。我遵循微软的这个指南。
However I don't have a model class for the AspNetUsers table in the localdb, for me to create a property on Insurance policy, eg public AspNetUsers User {get; set;}
但是,对于 localdb 中的 AspNetUsers 表,我没有 model class,以便我在保险单上创建一个属性,例如public AspNetUsers User {get; set;}
public AspNetUsers User {get; set;}
How do I do this?我该怎么做呢? Or is there a better way to do this?还是有更好的方法来做到这一点?
I'm new to Core so code first design as opposed to db first is still blurry for me.我是 Core 的新手,所以代码优先设计而不是 db first 对我来说仍然是模糊的。
UPDATE更新
Looks like the model for AspNetUsers table is IdentityUser.看起来 AspNetUsers 表的 model 是 IdentityUser。 Is this true, or am I still lost?这是真的,还是我仍然迷路?
For example, i will add model Book
and add a foreign key to Identity user, a book belongs to one user:例如,我将添加 model Book
并为 Identity 用户添加一个外键,一本书属于一个用户:
Create Book.cs
:创建Book.cs
:
public class Book { public int Id { get; set; } public string Name { get; set; } public string UserId { get; set; } public IdentityUser User { get; set; } }
Modify ApplicationDbContext
to include the DbSet:修改ApplicationDbContext
以包含 DbSet:
public class ApplicationDbContext: IdentityDbContext { public DbSet<Book> Books { get; set; } public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options) { } }
add-migration Name
and update-database
to create table and relationship in database. add-migration Name
和update-database
在数据库中创建表和关系。
If you want to add a book which belongs to current user in controller, you can firstly inject the services:如果要在 controller 中添加属于当前用户的书,可以先注入服务:
private readonly ILogger<HomeController> _logger; private readonly ApplicationDbContext _context; private readonly UserManager<IdentityUser> _userManager; public HomeController(ILogger<HomeController> logger, ApplicationDbContext context, UserManager<IdentityUser> userManager) { _logger = logger; _context = context; _userManager = userManager; }
Create book and save to database:创建书并保存到数据库:
Book book = new Book(); //or search user by FindByIdAsync or FindByEmailAsync var user = await _userManager.GetUserAsync(HttpContext.User); book.User = user; _context.Books.Add(book); await _context.SaveChangesAsync();
If you want to read book with User properties, you can try:如果您想阅读具有用户属性的书籍,可以尝试:
using Microsoft.EntityFrameworkCore; var result= _context.Books.Include(rt => rt.User).Where(x => x.Id == 1).FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.