Entity Framework Code First で複数のデータベース コンテキストを実装するのに苦労しています。写真に示すように、3 つのコンテキスト (すべてのエンティティ、会議に関連するエンティティ、およびユーザー プロファイルに関連するエンティティ) を既に実装しています。
Fluent API は、エンティティ マッピングに使用されます。たとえば、これは 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);
}
}
下の図に示すようなコンテキストを作成するのを手伝ってくれる人はいますか。