Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Change from 2 DbContext to 1

$
0
0

Hi

Apologies for what I am sure must be a very basic question...

I am using Asp.net Core with Identity and currently have 2 DbContext (ApplicationDbContexts and ToDoDbContext).
Since I am running into issues trying to create FK from Models in my ToDoDbContext to the ApplicationUser - I want "merge" my 2 db contexts into one.

This is what they currently look like (simplified):

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

        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);
        }

        public DbSet<ApplicationUser> ApplicationUser { get; set; }

        public DbSet<ApplicationRole> RoleManager { get; set; }
    }

and the ToDoDbContext:

  public class ToDoDbContext : DbContext
    {
        public ToDoDbContext(DbContextOptions<ToDoDbContext> options) : base(options)
        {
        }

        public DbSet<ToDo> ToDos { get; set; }

    }

There is already a lot of data in my database which off course I don't want to lose, while trying to merge these 2.

Can I simply cut and paste my DbSets in my ApplicationDbContext (like below) and then delete the ToDoDbContext?
Or how would one go about doing this?

Can I simply do this?

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

        public DbSet<ToDo> ToDos { 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);
        }

        public DbSet<ApplicationUser> ApplicationUser { get; set; }

        public DbSet<ApplicationRole> RoleManager { get; set; }
    }
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>