如何将Employees导航属性添加到已具有Manager导航属性的ASP.NET Identity ApplicationUser?

时间:2017-11-06 05:45:22

标签: c# asp.net asp.net-mvc asp.net-identity

我目前有ApplicationUser实体,可以从Identity框架扩展IdentityUser

[Table("User")]
public class ApplicationUser : IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
    [Column("CreatedDate"), DataType("datetime2")]
    public DateTime? CreatedOn { get; set; }
    [Column("CreatedBy")]
    public string CreatedBy { get; set; }

    [Column("ModifiedDate"), DataType("datetime2")]
    public DateTime? ModifiedOn { get; set; }
    [Column("ModifiedBy")]
    public string ModifiedBy { get; set; }

    [Column("LastLoginDate"), DataType("datetime2")]
    public DateTime? LastLoginOn { get; set; }

    [Column("TimeZoneID"), ForeignKey("TimeZone")]
    public string TimeZoneId { get; set; }
    [Column("RegionID"), ForeignKey("Region")]
    public string RegionId { get; set; }
    [Column("ManagerID"), ForeignKey("Manager")]
    public string ManagerId { get; set; }

    public virtual TimeZone TimeZone { get; set; }
    public virtual Region Region { get; set; }
    public virtual ApplicationUser Manager { get; set; }

    public virtual ICollection<Appointment> Appointments { get; set; }
    public virtual ICollection<OnCallGroup> OnCallGroups { get; set; }


    public ApplicationUser()
    {
        Id = Guid.NewGuid().ToString();
        Appointments = new HashSet<Appointment>();
        OnCallGroups = new HashSet<OnCallGroup>();
    }

    public ApplicationUser(string userName)
        : this(userName, DateTime.UtcNow)
    {
    }

    public ApplicationUser(string userName, DateTime createdOn)
        : this()
    {
        UserName = userName;
        CreatedOn = createdOn;
        Email = userName;
        EmailConfirmed = true;
    }

    private ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

它运作良好,我可以添加一个管理器属性,以防用户有一个管理员(检查以避免循环引用在业务层一侧完成,一个利用ApplicationUserManager和{{1} }。

我的ApplicationRoleManager已经修改如下,以便自定义连接表并更好地符合我的一些命名约定:

ApplicationDbContext

我想添加另一个导航属性以保存属于某些用户的技术人员或员工(例如管理角色),这是一个好主意吗?我该怎么办?

是否足以添加下面的行来添加另一个导航属性:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>
{
    private const string DefaultConnectionString = "ApplicationDbContext";

    public DbSet<Appointment> Appointments { get; set; }
    public DbSet<AppointmentCategory> AppointmentCategories { get; set; }

    public DbSet<TimeZone> TimeZones { get; set; }
    public DbSet<Region> Regions { get; set; }

    public DbSet<OnCallGroup> OnCallGroups { get; set; }

    public DbSet<Log> Logs { get; set; }

    public ApplicationDbContext()
        : base(DefaultConnectionString)
    {
    }

    static ApplicationDbContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, ContextMigrationConfiguration>(DefaultConnectionString));
    }

    public static ApplicationDbContext Create()
    {
        var applicationDbContext = new ApplicationDbContext();
        return applicationDbContext;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

        modelBuilder.Entity<ApplicationUser>().ToTable("User").Property(x => x.Id).HasColumnName("IDUser");
        modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRole").Property(x => x.RoleId).HasColumnName("RoleID");
        modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRole").Property(x => x.UserId).HasColumnName("UserID");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin").Property(x => x.UserId).HasColumnName("UserID");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim").Property(x => x.Id).HasColumnName("IDUserClaims");
        modelBuilder.Entity<ApplicationRole>().ToTable("Role").Property(x => x.Id).HasColumnName("IDRole");

        modelBuilder.Entity<ApplicationUser>().HasMany(x => x.OnCallGroups).WithMany(x => x.Users).Map(x =>
        {
            x.MapLeftKey("UserID");
            x.MapRightKey("OnCallGroupID");
            x.ToTable("UserOnCallGroup");
        });

        modelBuilder.Entity<ApplicationRole>().HasMany(x => x.AppointmentCategories).WithMany(x => x.Roles).Map(x =>
        {
            x.MapLeftKey("RoleID");
            x.MapRightKey("AppointmentCategoryID");
            x.ToTable("RoleAppointmentCategory");
        });
    }
}

0 个答案:

没有答案