Correct way to use two contexts in asp.net core with entity framework core

0

I'm trying to apply what I've been studying and I've caught up with a few contexts.

What I'm trying to do is to use the Microsoft Identity structure with my application entities.

Structure of solution :

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

I created an interface to keep the same new properties and thus have no 'migration ' problem of two classes for the same 'table'.

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

I applied to my User entity and 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 noted, I have separated the entities in a project. I'm also trying to separate the context into a project and that's where I stuck.

        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 made the contexts call:

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

You have presented the assembly error, which I researched by changing the service to the same assembly as the main project.

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

However, running the second context update generated the table error already exists.

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

My biggest question is how to solve this problem, which is the best and best way to use both contexts for this.

The user configuration class to stay the same as ApplicationUser did not show here because I do not think it is necessary, but if I need to, I'll show it as well.

    
asked by anonymous 29.08.2017 / 21:37

1 answer

0

The most correct way depends on the type of application you are developing. There will always be a trade-off. About its error, is why this table is already defined in the context of Idendity. You can have another user table that is referenced by the Identity user ID. Or use the same Identity DbContext to make your application.

    
09.09.2017 / 22:13