简体   繁体   中英

IdentityDbContext User DbSet Name

I've created a custom user inheriting from IdentityUser called Contacts, and my applications dbcontext inherits from IdentityDbContext like so:

public class Contact : IdentityUser<int, ContactLogin, ContactRole, ContactClaim>
{
    public Contact()
    {            
    }
}

public class dbcontext : IdentityDbContext<Contact, Role, int, ContactLogin, ContactRole, ContactClaim>
{
    public dbcontext()
        : base("dbcontext")
    {
    }        


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // IdentityDbContext base - must be called prior to changing identity configuration
        base.OnModelCreating(modelBuilder);

        // custom identity table names and primary key column Id names
        modelBuilder.Entity<Contact>().ToTable("Contacts").Property(p => p.Id).HasColumnName("ContactId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity<ContactRole>().ToTable("ContactRoles");
        modelBuilder.Entity<ContactLogin>().ToTable("ContactLogins");
        modelBuilder.Entity<ContactClaim>().ToTable("ContactClaims").Property(p => p.Id).HasColumnName("ContactClaimId");
        modelBuilder.Entity<Role>().ToTable("Roles").Property(p => p.Id).HasColumnName("RoleId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

By default IdentityDbContext contains a Users DbSet. Is it possible to change the name of this DbSet to match the type that it's implementing, eg Contacts?

It's not a big deal, but it would just be nice to refer to the DbSet using dbcontext.Contacts instead of dbcontext.Users.

Thanks.

The base IdentityDbContext uses: public virtual IDbSet<TUser> Users { get; set; } public virtual IDbSet<TUser> Users { get; set; } public virtual IDbSet<TUser> Users { get; set; } to expose the Users DbSet.

You'll need a similar property for your own implementation, eg: public IDbSet<Contacts> Contacts { get; set; } public IDbSet<Contacts> Contacts { get; set; }

Update

Question was regarding renaming the existing DbSet of Contacts from Users to Contacts.

No, you can't do this out of the box. You could attempt to wrap it and expose it again, but this isn't really the right thing to do. See this question for an in depth discussion .

Just a note that if you decide to overwrite anything or add your own, the default EF implementation of UserStore will use the DbSet named Users. Just something to keep an eye on if you get unexpected behavior.

Generally what I tend to do is have a big separation of concerns right.

So I have:

public IDbSet<User> Users { get; set; }

This represents anyone who wants to log into my system. So now I want to model actual concepts into my database, concepts that relate to real world things. So I have a system administrator for example, I will create an entity for this.

public class SystemAdministrator
{
   public string Name { get; set; }

   public int LocationId { get; set; } // a complex representation of where this administrator works from

   public int UserId { get; set; } // this is now a reference to their log in
}

Now my context will look like this:

public IDbSet<User> Users { get; set; }

public DbSet<SystemAdministrator> SystemAdministrators { get; set; } // I use DbSet because it exposes more methods to use like AddRange.

This means now my database has proper representations of real world concepts which is easy for everyone to develop against. I do the same for Clients or Employees .

This also means that I can move away from primitive obsession

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM