How to refer to the AspNetUser table(which is the default table created upon using AspNetCore.Identity;) in my Job Table in .NET Core 6
-
I have created tables using ASP.NET Core Identity. I need to refer my `AspNetUser` table to my `Job` table. I have created a `CustomIdentity` class which inherits from `IdentityUser`.
This is my code:
using Microsoft.AspNetCore.Identity; namespace InventoryManagement.Models { public class CustomIdentity :IdentityUser { public string IsEngineer { get; set; } public string authpassword { get; set; } } }
And this is my `DbContext` class for `IdentityDbContext`:
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace InventoryManagement.Models.Data { public class AuthDbContext : IdentityDbContext { public AuthDbContext(DbContextOptions options) :base(options) { } } }
`ASPNETUser` table got created and I am able to insert values into it using `UserManager` to register users. I need to refer this user in my Job table. And here is my code for `Jobdbcontext` where I declare all my other tables:
using Microsoft.EntityFrameworkCore; namespace InventoryManagement.Models.Data { public class JobDBcontext : DbContext { public JobDBcontext(DbContextOptions options) : base(options) { } public DbSet Jobs { get; set; } public DbSet Customers { get; set; } public DbSet Jobtype { get; set; } public DbSet Manufacturingbay { get; set; } public DbSet Projectcategory { get; set; } public DbSet Currency { get; set; } public DbSet ItemCategory { get; set; } public DbSet ItemBudget { get; set; } public DbSet ItemSubcategory { get; set; } public DbSet ItemMaster { get; set; } public DbSet Bom { get; set; } public DbSet Test { get; set; } public DbSet UomMaster { get; set; } public DbSet PR { get; set; } public DbSet PRDetails { get; set; } public DbSet Supplier { get; set; } public DbSet Purchase { get; set; } public DbSet PurchaseDetails { get; set; }
-
I have created tables using ASP.NET Core Identity. I need to refer my `AspNetUser` table to my `Job` table. I have created a `CustomIdentity` class which inherits from `IdentityUser`.
This is my code:
using Microsoft.AspNetCore.Identity; namespace InventoryManagement.Models { public class CustomIdentity :IdentityUser { public string IsEngineer { get; set; } public string authpassword { get; set; } } }
And this is my `DbContext` class for `IdentityDbContext`:
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace InventoryManagement.Models.Data { public class AuthDbContext : IdentityDbContext { public AuthDbContext(DbContextOptions options) :base(options) { } } }
`ASPNETUser` table got created and I am able to insert values into it using `UserManager` to register users. I need to refer this user in my Job table. And here is my code for `Jobdbcontext` where I declare all my other tables:
using Microsoft.EntityFrameworkCore; namespace InventoryManagement.Models.Data { public class JobDBcontext : DbContext { public JobDBcontext(DbContextOptions options) : base(options) { } public DbSet Jobs { get; set; } public DbSet Customers { get; set; } public DbSet Jobtype { get; set; } public DbSet Manufacturingbay { get; set; } public DbSet Projectcategory { get; set; } public DbSet Currency { get; set; } public DbSet ItemCategory { get; set; } public DbSet ItemBudget { get; set; } public DbSet ItemSubcategory { get; set; } public DbSet ItemMaster { get; set; } public DbSet Bom { get; set; } public DbSet Test { get; set; } public DbSet UomMaster { get; set; } public DbSet PR { get; set; } public DbSet PRDetails { get; set; } public DbSet Supplier { get; set; } public DbSet Purchase { get; set; } public DbSet PurchaseDetails { get; set; }
Navigation properties cannot span multiple
DbContext
types. If you want an entity type in one context to map to the same table as a type in a different context, then you will need to use theOnModelCreating
method to explicitly map it to the correct table. You may also need to exclude it from migrations. There's a good example in this blog post for EF Core 5 RC1: Announcing Entity Framework Core (EFCore) 5.0 RC1 - .NET Blog[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer