I am struggling to implement multiple database contexts in Entity Framework Code First. I have already implemented three contexts (all entities, entities related to conference and entities related to user profile) as shown on pictures.
Fluent API is used for entity mappings. For Example this is the code used for ConferenceContext:
public class ConferenceContext : BaseContext<ConferenceContext>
{
public DbSet<Conference> Conferences { get; set; }
public DbSet<ImportantDate> ImportantDates { get; set; }
public DbSet<Topic> Topics { get; set; }
public DbSet<Venue> Venues { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
insertEntityMappings(modelBuilder); // inserts entity mappings
}
private void insertEntityMappings(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new ConferenceMappings());
modelBuilder.Configurations.Add(new ImportantDateMappings());
modelBuilder.Configurations.Add(new TopicMappings());
modelBuilder.Configurations.Add(new VenueMappings());
modelBuilder.Ignore<ConferenceComment>();
modelBuilder.Ignore<ConferenceRating>();
modelBuilder.Ignore<ConnectionConferenceRecommendation>();
modelBuilder.Ignore<UserProfile>();
}
}
public class ConferenceMappings : EntityTypeConfiguration<Conference>
{
public ConferenceMappings()
{
// Primary Key
this.HasKey(t => t.ConferenceId);
this.Property(t => t.ConferenceId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(t => t.Title)
.IsRequired()
.HasMaxLength(150);
this.Property(t => t.Summary)
.HasMaxLength(1000);
this.Property(t => t.ContactEmail)
.HasMaxLength(100);
this.Property(t => t.WebsiteUrl)
.IsRequired()
.HasMaxLength(100);
this.Property(t => t.LogoUri)
.HasMaxLength(100);
// Table & Column Mappings
this.ToTable("Conference");
this.Property(t => t.ConferenceId).HasColumnName("ConferenceId");
this.Property(t => t.Title).HasColumnName("Title");
this.Property(t => t.Summary).HasColumnName("Summary");
this.Property(t => t.ContactEmail).HasColumnName("ContactEmail");
this.Property(t => t.WebsiteUrl).HasColumnName("WebsiteUrl");
this.Property(t => t.LogoUri).HasColumnName("LogoUri");
this.Property(t => t.BegginingDate).HasColumnName("BegginingDate");
this.Property(t => t.EndingDate).HasColumnName("EndingDate");
this.Property(t => t.VenueId).HasColumnName("VenueId");
// Relationships
this.HasMany(t => t.Topics)
.WithMany(t => t.Conferences)
.Map(m =>
{
m.ToTable("TopicConference");
m.MapLeftKey("Conference_ConferenceId");
m.MapRightKey("Topic_TopicId");
});
this.HasOptional(t => t.Venue)
.WithMany(t => t.Conferences)
.HasForeignKey(d => d.VenueId);
}
}
public class ImportantDateMappings : EntityTypeConfiguration<ImportantDate>
{
public ImportantDateMappings()
{
// Primary Key
this.HasKey(t => t.ImportantDateId);
this.Property(t => t.ImportantDateId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(t => t.EventName)
.IsRequired()
.HasMaxLength(100);
// Table & Column Mappings
this.ToTable("ImportantDate");
this.Property(t => t.ImportantDateId).HasColumnName("ImportantDateId");
this.Property(t => t.Date).HasColumnName("Date");
this.Property(t => t.EventName).HasColumnName("EventName");
// Relationships
this.HasRequired(t => t.Conference)
.WithMany(t => t.ImportantDates)
.HasForeignKey(t => t.ConferenceId);
}
}
public class TopicMappings : EntityTypeConfiguration<Topic>
{
public TopicMappings()
{
// Primary Key
this.HasKey(t => t.TopicId);
this.Property(t => t.TopicId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(t => t.Name)
.IsRequired()
.HasMaxLength(100);
// Table & Column Mappings
this.ToTable("Topic");
this.Property(t => t.TopicId).HasColumnName("TopicId");
this.Property(t => t.Name).HasColumnName("Name");
}
}
public class VenueMappings : EntityTypeConfiguration<Venue>
{
public VenueMappings()
{
// Primary Key
this.HasKey(t => t.VenueId);
this.Property(t => t.VenueId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Properties
this.Property(t => t.Address)
.HasMaxLength(100);
this.Property(t => t.City)
.HasMaxLength(50);
this.Property(t => t.State)
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("Venue");
this.Property(t => t.VenueId).HasColumnName("VenueId");
this.Property(t => t.Address).HasColumnName("Address");
this.Property(t => t.City).HasColumnName("City");
this.Property(t => t.State).HasColumnName("State");
this.Property(t => t.CountryId).HasColumnName("CountryId");
// Relationships
this.HasOptional(t => t.Country)
.WithMany(t => t.Venues)
.HasForeignKey(d => d.CountryId);
}
}
Can anyone help me to create Context that is similar to one shown on the picture below.