Correct way to use two contexts in Asp.net core with Entity framework core

Asked

Viewed 470 times

1

I’m trying to apply what I’ve been studying and stuck in a part of contexts.

What I’m trying to apply is using the Microsoft structure Identity with my application entities.

Structure of Solution:

- BaseFull.Web
- BaseFull.Entities
- BaseFull.Infra.Data

I created an interface to maintain the same new properties and so have no problem of 'Migration' of two classes for the same 'table'.

    public interface IUsuario
    {
        string Fullname { get; set; }
    }

I applied to my User entity and to Applicationuser:

namespace BaseFull.Web.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser,  IUsuario
    {
        public string Fullname { get; set; }
    }
}


namespace BaseFull.Entities.Models
{
    public class Usuario : IUsuario
    {
        public string Fullname { get; set; }
        public Usuario()
        {
            Id = Guid.NewGuid().ToString();
        }
        #region Propriedades da AspnetUsers(Identity)
        public string Id { get; set; }
        public virtual string Email { get; set; }
        public virtual string UserName { get; set; }


  ...
        #endregion
    }

As you have noticed, I have separated the entities into a project. I am also trying to separate the context into a project and this is where I traversed.

        namespace BaseFull.Infra.Data.Context
    {
        public class BaseFullContext: DbContext
        {
            public DbSet<Usuario> Usuarios { get; set; }
....

namespace BaseFull.Web.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<ApplicationUser>().ToTable("Usuarios");

In the Startup class of the Web project I called the contexts:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddDbContext<BaseFullContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));....

Presented the error of Assembly, which searching solved by changing the service to the same Assembly of the main project.

services.AddDbContext<BaseFullAppContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
                                , b => b.MigrationsAssembly("BaseFull.Web"))

However, when executing the second context update, the table error already exists.

 There is already an object named 'Usuarios' in the database.

My biggest question is how to solve this problem, what is the most correct and best way to use both contexts for it.

The User configuration class to remain the same as the Applicationuser I did not show here because I don’t think it is necessary, but if I need it I also show.

1 answer

0

The most correct way depends on the type of application you are developing. There has always been a trade-off. About your error, it is because this table is already defined in the Idendity context. You can have another user table that references the Identity user id. Or use the same Identity Dbcontext to make your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.