1

我正在努力在 Entity Framework Code First 中实现多个数据库上下文。如图所示,我已经实现了三个上下文(所有实体、与会议相关的实体和与用户配置文件相关的实体)。 所有实体

与会议相关的实体

与用户资料相关的实体

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);
    }
}

谁能帮我创建类似于下图所示的上下文。

新语境

4

0 回答 0