I have two databases, the first for user names and their data, and the other database is for training, and I did not find a way to link the trainee from the training table and the users table in the first database for users. Two Contexts were used, but I did not find the solution.
CentralBase db context:
public partial class CentralBase : DbContext
{
public CentralBase()
{
}
public CentralBase(DbContextOptions<CentralBase> options)
: base(options)
{
}
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("data source =.\\SQLEXPRESS;initial catalog=CentralBase;integrated security=SSPI;TrustServerCertificate=True");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AspNetUser>(entity =>
{
entity.ToTable("AspNetUser");
entity.HasKey(e => e.Id);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Training db context:
public partial class Training : DbContext
{
public Training()
{
}
public Training(DbContextOptions<Training> options)
: base(options)
{
}
public virtual DbSet<TrainingPrograme> TrainingPrograme { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("data source =.\\SQLEXPRESS;initial catalog=Training;integrated security=SSPI;TrustServerCertificate=True");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TrainingPrograme>(entity =>
{
entity.ToTable("TrainingPrograme");
entity.HasOne(p => p.AspNetUser)
.WithMany(u => u.TrainingPrograme)
.HasForeignKey(p => p.IdUserTraining);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Table AspNetUser :
public partial class AspNetUser
{
[Key]
public string Id { get; set; } = null!;
public string? UserName { get; set; }
public virtual ICollection<EduQ2024.Areas.Train.Data.TableTraining.TrainingPrograme> TrainingPrograme { get; set; } = new List<EduQ2024.Areas.Train.Data.TableTraining.TrainingPrograme>();
}
Table TrainingPrograme:
public partial class TrainingPrograme
{
public int Id { get; set; }
public string? IdUserTraining { get; set; }
public EduQ2024.data.TableCentral.AspNetUser AspNetUser { get; set; }
}
Test code
Data.DbTraining.Training Cn = new Data.DbTraining.Training();
var f = Cn.TrainingPrograme.ToList();
var g = f.Where(L => L.AspNetUser != null).ToList();.
Result:
AspNetUser = null