简体   繁体   English

IdentityDbContext用户DbSet名称

[英]IdentityDbContext User DbSet Name

I've created a custom user inheriting from IdentityUser called Contacts, and my applications dbcontext inherits from IdentityDbContext like so: 我创建了一个从IdentityUser继承的自定义用户,称为Contacts,而我的应用程序dbcontext则从IdentityDbContext继承,如下所示:

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. 默认情况下,IdentityDbContext包含一个用户DbSet。 Is it possible to change the name of this DbSet to match the type that it's implementing, eg Contacts? 是否可以更改此DbSet的名称以匹配其实现的类型,例如“联系人”?

It's not a big deal, but it would just be nice to refer to the DbSet using dbcontext.Contacts instead of dbcontext.Users. 没什么大不了的,但是使用dbcontext.Contacts而不是dbcontext.Users引用DbSet会很好。

Thanks. 谢谢。

The base IdentityDbContext uses: public virtual IDbSet<TUser> Users { get; set; } 基本IdentityDbContext使用: 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. public virtual IDbSet<TUser> Users { get; set; }以暴露用户DbSet。

You'll need a similar property for your own implementation, eg: public IDbSet<Contacts> Contacts { get; set; } 您需要为自己的实现使用类似的属性,例如: 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. 问题是有关将现有的DbSet联系人从用户重命名为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. 请注意,如果您决定覆盖任何内容或添加自己的内容,则UserStore的默认EF实现将使用名为Users的DbSet。 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 . 我对ClientsEmployees

This also means that I can move away from primitive obsession 这也意味着我可以摆脱原始的迷恋

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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