Domanda

In MVC 5 there is a new Identity Class in the models folder. SO let's say I add some additional fields to the user, I would do this in the class

public class ApplicationUser : IdentityUser

Ok, so far so good. I run add-migration "updatedUser" and that will update the database.

BUT... Now I want to add a Products, Client and Company Table. Now, some ASP.NET tutorials and Azure tutorials have actually had me setting the getters and setters for each Table inside the ApplicationDbContext class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

        public DbSet<Product> Products { get; set; }
        public DbSet<Client> Clients { get; set; }
        public DbSet<Company> Companies { get; set; }

        public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

But I would think you would not want to inherit from that IdentityDbContext and instead set up a file inside the models folder called:

DatabaseContext.cs (For non user/Identity tables)

and do something like:

    using MyProject.Domain.Entities;
    using System.Data.Entity;
    namespace MyProject.Models {

    public class DatabaseContext : DbContext 
        : base("DefaultConnection")
    {
    }

     public DbSet<Product> Products { get; set; }
     public DbSet<Client> Clients { get; set; }
     public DbSet<Company> Companies { get; set; }
}

Would this be best practice or should I use the Identity Class? Does it matter?

È stato utile?

Soluzione

Why would you think you would want to do that?

You aren't inheriting anything you aren't already doing because of the ApplicationDbContext supplied.

Adding a second context may make sense in some situations, but chances are you will want to access the Identity fields, and if you are using a different context then you can run into problems with context coherency.

And, FYI, you don't put your DbSet's in the ApplicationDbContext's constructor.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top