我一直在為我的實習開發一個專案,我可能設定了不正確的結構,因為在專案開發程序中不斷要求進行額外的更新。我的專案中有兩個不同的背景關系;一個用于身份,另一個用于我自己的物體。我的問題是:我想根據 EntityLayer 中的 CustomUser 創建身份表(這里沒問題),然后我想將身份(即用戶)與我自己的物體相關聯。雖然我沒有在 dataaccess 的背景關系中定義 customusers,但它會創建 CustomUser 表并與它建立關系,但我想要的是用 Identity 設定它。

和這里一樣,與 EmployeeDemands、EmployeeServices 或其他表的關系應該直接用 AspNetUsers 建立,而不是 CustomUser,我無法解決這個問題。
身份的 UI 層背景關系中的代碼。
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; }
}
}
物體層自定義用戶
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; }
}
}
例如,我試圖在 EmployeeServices 表中建立關系。
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; }
}
}
uj5u.com熱心網友回復:
您應該像下面這樣配置Users表格:ApplicationDbContext
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.
}
}
您可能需要重新創建資料庫。而另一方面,您可能還想忽略為DbContext中的CustomUser物體生成的遷移。Context要實作這一點,您應該在DbContext中配置CustomUser物體:Context
builder.Entity<CustomUser>().ToTable("CustomUser", build => build.ExcludeFromMigrations());
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/435871.html
