如何在 ASP.NET 中将业务模型与身份模型分开?

Posted

技术标签:

【中文标题】如何在 ASP.NET 中将业务模型与身份模型分开?【英文标题】:How to separate business models from Identity models in ASP.NET? 【发布时间】:2021-05-11 16:32:34 【问题描述】:

我正在使用实体框架和身份框架构建错误跟踪器。到目前为止,它包含三个模型,ApplicationUser、Projects 和 Ticket。 ApplicationUser 模型是 Identity 模型,而 Projects 和 Tickets 是业务模型。我不希望在进行迁移时将 Projects 和 Tickets 模型表示为用户,那么我将如何将这些模型彼此分离?我见过人们为他们的 Identity 类创建一个单独的类库,但不确定这是否可行。

错误:

没有为此 DbContext 配置数据库提供程序。可以通过覆盖“DbContext.OnConfiguring”方法或在应用程序服务提供程序上使用“AddDbContext”来配置提供程序。如果使用了“AddDbContext”,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions 对象并将其传递给 DbContext 的基本构造函数

Startup.cs

public void ConfigureServices(IServiceCollection services)

    services.AddControllersWithViews();
    services.AddDbContext<UsersContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("IssueTrackerConnection")));

    // In production, the React files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
            
                configuration.RootPath = "ClientApp/build";
            );

    // services.AddScoped<IIssueTrackerRepo, SqlUsersRepo>();
    services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
    services.AddControllers().AddNewtonsoftJson();
    services.AddRazorPages();
    services.AddDistributedMemoryCache();
    services.AddSession();
          
    services.AddDefaultIdentity<ApplicationUser>()
                .AddEntityFrameworkStores<UsersContext>()
                .AddDefaultTokenProviders();

IdentityDbContext类:

using IssueTracker.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace IssueTracker.Data

    public class UsersContext : IdentityDbContext<ApplicationUser>
    
        public UsersContext()
        
        

        public UsersContext(DbContextOptions<UsersContext> opt) : base(opt)
        
        

        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);

        

        public DbSet<ApplicationUser> ApplicationUser  get; set; 
        public DbSet<Projects> Projects  get; set; 
        public DbSet<Tickets> Tickets  get; set; 
    

ApplicationUser模型类:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;

namespace IssueTracker.Models

    public class ApplicationUser : IdentityUser
    
        public ApplicationUser()
        
        

        [Required]
        public string ApplicationUserusername  get; set; 

        [Required]
        public string password  get; set; 

        [Required]
        public string role  get; set; 

        public string Projectsname  get; set; 

        [ForeignKey("Projectsname")]
        public virtual Projects Projects  get; set; 

        public ApplicationUser(string ApplicationUserusername, string password, string role, string Projectsname)
        
            this.ApplicationUserusername = ApplicationUserusername;
            this.role = role;
            this.password = password;
            this.Projectsname = Projectsname;
        
    

 

Projects模型类:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;

namespace IssueTracker.Models

    public class Projects
    
        public Projects(string Projectsname, string description, string password)
        
            this.Projectsname = Projectsname;
            this.description = description;
            this.password = password;
        

        public Projects()
        
        

        [Key]
        [Required]
        public string Projectsname  get; set; 

        [Required]
        public string description  get; set; 

        public string ApplicationUserusername  get; set; 

        [ForeignKey("ApplicationUserusername")]
        public virtual ApplicationUser ApplicationUser  get; set; 

        public string password  get; set; 
    

Tickets模型类:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;

namespace IssueTracker.Models

    public class Tickets
    
        [Key]
        [Required]
        public string title  get; set; 
        
        [Required]
        public string submitter  get; set; 

        [Required]
        public string developer  get; set; 

        [Required]
        public string status  get; set; 

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "0:dd/MM/yyyy")]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public DateTime? created  get; set; 

        public string? Projectsname  get; set; 

        [ForeignKey("Projectsname")]
        public virtual Projects Projects  get; set; 

        public Tickets(string title, string submitter, string developer, string status, DateTime created, string Projectsname)
        
            this.title = title;
            this.submitter = submitter;
            this.developer = developer;
            this.status = status;
            this.created = created;
            this.Projectsname = Projectsname;
        

        public Tickets()
        
        
    

appsettings.json:


  "Logging": 
    "LogLevel": 
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    
  ,
  "AllowedHosts": "*",
  "ConnectionStrings":
  
    "IssueTrackerConnection": "Server=localhost, 1433; Initial Catalog=IssueTrackerDB; User ID=sa; Password=***********"
  ,

【问题讨论】:

我不太了解您的需求。是否要生成不包含 IdentityUser 属性的项目和工单? 为什么要项目和工单继承 IdentityUser? @Yinqiu 所以这是我的错,我不希望项目和工单都从 IdentityUser 继承,因为它们是商业模式,我只是用它来测试。我希望这两个模型都与我的 ApplicationUser 分开,这样当我进行迁移时,只有 AppUser 是一个身份模型。我应该将这些模型放在类库中吗? 您现在所做的迁移生成的表将不相关。 您可以尝试迁移。并检查数据库。 【参考方案1】:

首先你可以创建一个新工作:

然后添加你的模型。改变你的上下文和你的启动就像你的代码一样。

上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    public ApplicationDbContext()
    

    
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    
    
    protected override void OnModelCreating(ModelBuilder builder)
    
        base.OnModelCreating(builder);
    
    public DbSet<ApplicationUser> ApplicationUser  get; set; 
    public DbSet<Projects> Projects  get; set; 
    public DbSet<Tickets> Tickets  get; set; 

启动:

public void ConfigureServices(IServiceCollection services)
    
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDatabaseDeveloperPageExceptionFilter();

        services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddControllersWithViews();
    

把你的connectionString改成你自己。

然后迁移。

结果:

那你可以比较一下这个项目和你的区别。由于我无法知道你项目的结构,所以很难找到你的问题。

【讨论】:

以上是关于如何在 ASP.NET 中将业务模型与身份模型分开?的主要内容,如果未能解决你的问题,请参考以下文章

asp.net core系列 48 Identity 身份模型自定义

ASP.NET MVC

在 ASP.NET Core MVC6 中将文件和模型发布到控制器

如何在 ASP.NET Core 中将角色添加到 Windows 身份验证

在 ASP.NET Core 2.0 中使用带有身份模型的 Azure Active Directory OAuth

在 asp.net 的视图模型中添加业务逻辑是不是会增加传递给视图的对象的大小?