Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Core: Add data to IdentityDbContext or use DbContext

I work with Asp.Net Core WebApi project.

Can I add my tables to IdentityDbContext, like this:

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

        public DbSet<ProgrammerRole> ProgrammerRoles { get; set; }

        public DbSet<Project> Projects { get; set; }
        public DbSet<SubProject> SubProjects { get; set; }

        public DbSet<Report> Reports { get; set; }
    }

Or do I need to create a second DbContext. And if i create a second DbContext how can I communicate wiht User in IdentityDbContext.

And my second question: If i add data in IdentityDbContext, like above, How do I get the data from my tables in ApplicationDbContext? Because i need to pass DbContextOptions object every time I create a new instance оf ApplicationDbContext. I do this in Startup.cs:

// ===== Add DbContext ========
            var connectionString = Configuration.GetConnectionString("DbConnection");
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(connectionString));

I saw in older version of Asp.Net Core, that i can pass Connection String in IdentityDbContext constructor, but now only DbContextOptions. And i can't do, for example this:

[AllowAnonymous]
        [HttpGet]
        public IEnumerable<Project> GetRoles()
        {
            using (var db = new ApplicationDbContext())
            {
                return db.Projects;
            }
        }
like image 242
Nikita Goncharuk Avatar asked May 16 '18 18:05

Nikita Goncharuk


1 Answers

Can I add my tables to IdentityDbContext, like this:

Yes, it is how you create custom tables. You do not need to create another DbContext. E.g.

public class Project
{
    public int Id { get; set; }
    public string Name { get; set; }
}

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

    public DbSet<Project> Projects { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Project>(entity =>
        {
            entity.Property(e => e.Name)
                .IsRequired()
                .HasMaxLength(50);
        });

        base.OnModelCreating(builder);
    }
}

Note: you might need to run dotnet ef migrations add Initial and dotnet ef database update for database migration.

using (var db = new ApplicationDbContext()) {...}

You should not create or manage ApplicationDbContext inside controller. If you do so, they become tightly coupled, and you cannot implement unit tests.

Instead, you let dependency inversion (DI) container manage it for you. E.g.

public class UserController : Controller
{
    private readonly ApplicationDbContext _context;

    public UserController(ApplicationDbContext context)
    {
        _context = context;
    }

    [AllowAnonymous]
    [HttpGet]
    public IEnumerable<Project> GetRoles()
    {
        return _context.Projects;
    }
}
like image 68
Win Avatar answered Nov 11 '22 05:11

Win