Reputation: 253
I am building a Bug Tracker with Entity Framework and the Identity framework. It contains three models so far, ApplicationUser, Projects, and Tickets. The ApplicationUser model is an Identity model, while Projects and Tickets are business models. I don't want the Projects and Tickets models to be represented as users when I make migrations, so how would I go about separating these models from each other? I've seen people creating a separate class library for their Identity classes, but am not sure if that will work.
Error:
No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for 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
class:
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
model class:
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
model class:
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
model class:
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=***********"
},
}
Upvotes: 0
Views: 326
Reputation: 7200
First you can create a new job:
Then add your model.change your context and your startup like your code.
Context:
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; }
}
Startup:
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();
}
Change your connectionString to yourself.
Then migrate.
Result:
Then you can compare the differences in this project and yours.Since I have no way of knowing the structure of your project, it is difficult to find your problem.
Upvotes: 1