简体   繁体   English

ASP.NET Core Web Application: create model class that has default User model as property

[英]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 用户添加一个外键,一本书属于一个用户:

  1. 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; } }
  2. 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) { } }
  3. add-migration Name and update-database to create table and relationship in database. add-migration Nameupdate-database在数据库中创建表和关系。

  4. 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; }
  5. 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();
  6. 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.

相关问题 如何使用 asp.net 内核在 model class 中创建通用属性 - How to create generic property in model class using asp.net core asp.net core web api 2中的抽象类模型绑定 - Abstract class model binding in asp.net core web api 2 具有继承关系的模型的asp.net mvc默认模型绑定程序 - asp.net mvc default model binder for model that has inheritance 当模型属性具有 [ConcurrencyCheck] 属性时,ASP.NET Core MVC ConcurrencyCheck 失败 - ASP.NET Core MVC ConcurrencyCheck is failing when the model property has the [ConcurrencyCheck] attribute 在 ASP.NET 核心 Web ZDB974238714CA8ADE3CE638A 中的 HttpGet 方法中返回 Model 的单个属性 - Returning a single property of a Model in HttpGet Method in ASP.NET Core Web API 如何防止通过 ASP.NET Core Web API 中的模型绑定设置属性? - How to prevent a property from being set through model binding in ASP.NET Core Web API? ASP.NET 内核 3.1 Web Api 对 Z20F35E630DAF449DBFA4C3F6885 级别的授权 - ASP.NET Core 3.1 Web Api authorization on model property level 如何将ASP.net的默认“应用程序用户”附加到特定于应用程序的模型? - How do I attach ASP.net's default Application User to application specific model? ASP.NET Core MVC默认模型值 - ASP.NET Core MVC default model value 如何在ASP.NET Core中增强ModelBinder(更新模型类的属性值) - How can I enhance ModelBinder in ASP.NET Core (update property values for a model class)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM