【发布时间】: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 是一个身份模型。我应该将这些模型放在类库中吗?
-
您现在所做的迁移生成的表将不相关。
-
您可以尝试迁移。并检查数据库。
标签: c# asp.net-core entity-framework-core asp.net-core-mvc