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

asp.net asp.net-core asp.net-identity entity-framework-6 entity-framework-core

Question

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;
            }
        }
1
4
5/16/2018 6:23:45 PM

Accepted Answer

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;
    }
}
8
5/16/2018 7:57:48 PM

Popular Answer

I solve my problem, i just replaced code in my ApplicationDbContext, and get connection string from method:

public class ApplicationDbContext : IdentityDbContext<User>
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(GetConnectionString());
        }

        private static string GetConnectionString()
        {
            const string databaseName = "EmployeeReportsDb";
            const string databasePass = "SuperPuper_Random_DB-key!";

            return $"Server=localhost;" +
                   $"database={databaseName};" +
                   $"Trusted_Connection = True;" +
                   $"MultipleActiveResultSets = True;" +
                   $"pwd={databasePass};" +
                   $"pooling=true;";
        }

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

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

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

here is the resource: https://medium.com/@ozgurgul/asp-net-core-2-0-webapi-jwt-authentication-with-identity-mysql-3698eeba6ff8



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow