Answer the question
In order to leave comments, you need to log in
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);
}
}
public class ImagesContext : IdentityContext
{
public ImagesContext(DbContextOptions<ImagesContext> options) : base(options) { }
public DbSet<ImgBuffer> ImgBuffer { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ImagesContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question