I have been developing a project for my internship for a while and I may have set up the structure incorrectly as additional updates are constantly requested during the project development process. There are 2 separate contexts in my project; one for Identity and the other for my own entities. My problem is: I want to create Identity tables according to CustomUser in EntityLayer (no problem here) and then I want to associate Identity (ie user) with my own entities. Although I haven't defined customusers in the context in dataaccess here, it goes and creates the CustomUser table and establishes the relations with it, but what I want is to set it up with Identity.
As here, relations with EmployeeDemands, EmployeeServices or other tables should be established directly with AspNetUsers, not CustomUser, and I could not solve this problem.
Code in UI Layer Context for Identity.
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.AspNetCore.Identity;
namespace CompanyPanelUI.Data
{
public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace DataAccessLayer.Concrete
{
public class Context:DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("server=(localdb)\\MSSQLLocalDB;database=CompanyPanelNew_DB; integrated security=true;");
}
public DbSet<Demand> Demands {get; set;}
public DbSet<Firm> Firms {get; set;}
public DbSet<Service> Services {get; set;}
public DbSet<FirmService> FirmServices {get; set;}
public DbSet<User> Users { get; set;}
public DbSet<DemandAnswer> DemandAnswers { get; set;}
public DbSet<DemandFile> DemandFiles { get; set;}
public DbSet<Department> Departments { get; set; }
public DbSet<DepartmentEmployee> DepartmentEmployees { get; set; }
public DbSet<EmployeeService> EmployeeServices { get; set; }
public DbSet<EmployeeDemand> EmployeeDemands { get; set; }
}
}
EntityLayer CustomUser
using EntityLayer.Concrete;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
namespace EntityLayer.Concrete
{
public class CustomUser:IdentityUser
{
[Display(Name = "Ad Soyad")]
public string NameSurname { get; set; }
[Display(Name = "Başvurulan Şirket")]
public string ApplicationFirm { get; set; }
[Display(Name = "Firma Id")]
public int? FirmId { get; set; }
[Display(Name = "Bolum Id")]
public int? DepartmentId { get; set; }
}
}
As an example, the relationship I'm trying to establish in the EmployeeServices table.
using System.ComponentModel.DataAnnotations;
namespace EntityLayer.Concrete
{
public class EmployeeService
{
[Key]
public int EmployeeServicesId { get; set; }
public int ServiceId { get; set; }
public Service Services { get; set; }
public string Id { get; set; }
public CustomUser User { get; set; }
}
}
CodePudding user response:
You should configure the Users
table in your ApplicationDbContext
like below :
public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<CustomUser>().ToTable("CustomUser");
// This will configure the table name of the Identity users table.
// If you wish you can rename the other tables too.
}
}
You may want to recreate the database.
And a side not you also may want to ignore the migrations generated for the CustomUser
entity in the Context
DbContext. To Achieve this you should configure the CustomUser
entity in the Context
DbContext:
builder.Entity<CustomUser>().ToTable("CustomUser", build => build.ExcludeFromMigrations());