如何在模型中使用 .NET Identity 脚手架用户
How to use .NET Identity scaffold users in models
我有一个代表项目的模型,一个项目应该有一个或多个用户附加到它。我的项目中有标识脚手架,我的 applicationDB 上下文继承自 identityDBContext。我想在我自己的一个模型中使用脚手架附带的用户“模型”。我怎样才能做到这一点?
我的应用程序数据库上下文
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Project> projects { get; set; }
public DbSet<Phase> phases { get; set; }
public DbSet<Models.Task> tasks { get; set; }
}
我的项目模型
public Project()
{
this.Users = new HashSet<???>();
}
[Key]
public int Id { get; set; }
public string Title { get; set; }
public int Description { get; set; }
public ICollection<???> Users { get; set; }
}
在看到您的描述和代码之间的内容后,我建议您执行以下步骤。
Let's begin with your statement "I want to use the user "model" that comes with the scaffold in one of my own models"
Inherit your IdentityUser class in Custom User Class:
public class User : IdentityUser
{
public string NewCustomProperty { get; set; }
public string AnotherCustomProperty { get; set; }
}
Note:
Remember that we are inheriting this for our implementation flexibility so that our base IdentityUser
class
doesn't get impacted. Therefore, Its not necessary that you have to
have property in your derived User
class.You can make it empty.
Then Inherit your IdentityDbContext class in your ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Project> projects { get; set; }
public DbSet<Phase> phases { get; set; }
public DbSet<Models.Task> tasks { get; set; }
}
Note:
Make sure while you are Inheriting
IdentityUser class
make sure you have added the correct namespace
which is using Microsoft.AspNetCore.Identity
Your Project Model Now Should be:
public class Project
{
public Project()
{
Users = new HashSet<User>();
}
[Key]
public int Id { get; set; }
public string Title { get; set; }
public int Description { get; set; }
public ICollection<User> Users { get; set; }
}
Note:
I hope it will help you implement what you are trying to
accomplish.
我有一个代表项目的模型,一个项目应该有一个或多个用户附加到它。我的项目中有标识脚手架,我的 applicationDB 上下文继承自 identityDBContext。我想在我自己的一个模型中使用脚手架附带的用户“模型”。我怎样才能做到这一点?
我的应用程序数据库上下文
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Project> projects { get; set; }
public DbSet<Phase> phases { get; set; }
public DbSet<Models.Task> tasks { get; set; }
}
我的项目模型
public Project()
{
this.Users = new HashSet<???>();
}
[Key]
public int Id { get; set; }
public string Title { get; set; }
public int Description { get; set; }
public ICollection<???> Users { get; set; }
}
在看到您的描述和代码之间的内容后,我建议您执行以下步骤。
Let's begin with your statement
"I want to use the user "model" that comes with the scaffold in one of my own models"
Inherit your IdentityUser class in Custom User Class:
public class User : IdentityUser
{
public string NewCustomProperty { get; set; }
public string AnotherCustomProperty { get; set; }
}
Note:
Remember that we are inheriting this for our implementation flexibility so that our baseIdentityUser
class doesn't get impacted. Therefore, Its not necessary that you have to have property in your derivedUser
class.You can make it empty.
Then Inherit your IdentityDbContext class in your ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public DbSet<Project> projects { get; set; }
public DbSet<Phase> phases { get; set; }
public DbSet<Models.Task> tasks { get; set; }
}
Note:
Make sure while you areInheriting
IdentityUser class
make sure you have added the correctnamespace
which isusing Microsoft.AspNetCore.Identity
Your Project Model Now Should be:
public class Project
{
public Project()
{
Users = new HashSet<User>();
}
[Key]
public int Id { get; set; }
public string Title { get; set; }
public int Description { get; set; }
public ICollection<User> Users { get; set; }
}
Note:
I hope it will help you implement what you are trying to accomplish.