V
V
Victor P.2018-12-04 10:18:22
.NET
Victor P., 2018-12-04 10:18:22

How to correctly configure entity context inheritance?

Good afternoon!
I decided to deal with authentication and made a project from scratch (.net core). I want to make it so that the context of the main database is inherited from the context of my users, so that I can reuse it in other projects.
I create an IdentityContext, inheriting from DbContext:

public class IdentityContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Profile> Profiles { get; set; }
        public DbSet<Role> Roles { get; set; }

        public IdentityContext(DbContextOptions options)
            : base(options)
        {
            Database.EnsureCreated();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder
                .Entity<User>()
                .HasOne(u => u.Profile)
                .WithOne(p => p.User)
                .HasForeignKey<Profile>(p => p.Id);

            modelBuilder.Entity<UserInRole>()
            .HasKey(t => new { t.UserId, t.RoleId });

            modelBuilder.Entity<UserInRole>()
                .HasOne(sc => sc.User)
                .WithMany(s => s.UserInRoles)
                .HasForeignKey(sc => sc.UserId);

            modelBuilder.Entity<UserInRole>()
                .HasOne(sc => sc.Role)
                .WithMany(c => c.UserInRoles)
                .HasForeignKey(sc => sc.RoleId);
        }
    }

I create the main database (I am planning a site with pictures) and inherit from the created IdentityContext:
public class ImagesContext : IdentityContext
    {
        public ImagesContext(DbContextOptions<ImagesContext> options) : base(options) { }

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

In Startup.cs I add a config for the context:
public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ImagesContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddMvc();
        }

When trying to update-database, an error occurs that the "ImgBuffer" table cannot be added because it already exists. I climb on the server, the basis is created, tables are created.
But this error reports that something is done wrong.
The code that is in the .net framework mvc template with individual user authentication is too complicated for me yet, although it does exactly what I need.
What is causing my error? Maybe I'm passing options through constructors incorrectly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor P., 2018-12-13
@Jeer

Oh, I found my question without answers in the mailing list)
In general, somehow sluggishly, absolutely not a single answer, although the code is posted and the problem is indicated.
It was in the line:
I honestly copied it from the tutorials without understanding what it does.
Actually, I never looked at what she was doing, the name seemed to say) After deletion, everything worked without errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question