Search code examples
asp.net-mvcentity-frameworkef-code-firstasp.net-identityentity-framework-migrations

Add new entities to DbContext


I use ASP.NET Core with Identity and want to extend default Db context. If I want to add not linked table I just add a new class:

public partial class Table1
{
    public int Id { get; set; }
    public string Txt { get; set; }
}

and extend my ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Table1> Table1 { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<Table1>(entity =>
        {
            entity.ToTable("Table_1");

            entity.Property(e => e.Id).HasColumnName("ID");

            entity.Property(e => e.Txt)
                .IsRequired()
                .HasMaxLength(50);
        });
    }
}

then create a migration and update db. It works. But if I want to add a new table, which linked to table from IdentityDbContext:

public partial class Users
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual AspNetUser User { get; set; }
}

of course, AspNetUser class does not exist (it's created by IdentityDbContext, as I understand). How to do it correctly?


Solution

  • The class is most likely named ApplicationUser (the default). The table that represents this entity is dbo.AspNetUsers, but that is set by Identity, and has nothing to do with the class name.

    FWIW, though, it's a bad idea to create a Users entity, for a number of reasons:

    1. There will undoubtedly be confusion between Users and ApplicationUser, as well as the database tables dbo.Users and dbo.AspNetUsers.

    2. In general, you should name your entities in singular tense, i.e. User, not Users. There's a whole host of reasons for this convention, but suffice to say, it just makes your code better and more readable to stick to singular tense for singular things and plural tense for plural things. For example, a property of type ICollection<User> would be named Users, since it's composed of many User instances.

    3. What you're doing is completely unnecessary. The whole reason for Identity's existence is that Membership (the previous authentication and authorization framework employed by ASP.NET) did not allow you to extend the types involved. Identity changes all this and is 100% extensible in every way. You have full access to all entities involved in the the framework and you can add to them and derive from them. If you want to add additional properties for "users" in your system, just add them to the ApplicationUser class directly.