简体   繁体   中英

Why do all of my model classes get added to the first Configuration.cs file that gets created when I'm using multiple Data Context classes?

Entity Framework 6 Code First Migrations with Multiple Data Contexts

NOTE: This is my first post on Stack Overflow so please let me know if I need to provide more detail.

FYI: I have read the article:

Entity Framework 6 Code First Migrations with Multiple Data Contexts

And I'm trying to do something similar. My question/problem revolves around the CreateTable methods generated in the Configuration.cs file.

I have 29 model classes, and I'm trying to set up 4 different Data Contexts.

In my first Data Context, RulingRequestContext, I have 11 of my 29 model classes:

public class RulingRequestContext : BaseContext<RulingRequestContext>
{
    public DbSet<RulingRequest> RulingRequests { get; set; }
    public DbSet<Agency> Agencies { get; set; }
    public DbSet<RulingRequestGroup> RulingRequestGroups { get; set; }
    public DbSet<RulingRequestOverallOutcome> RulingRequestOverallOutcomes { get; set; }
    public DbSet<RulingRequestType> RulingRequestTypes { get; set; }
    public DbSet<RulingRequestResult> RulingRequestResults { get; set; }
    public DbSet<Issue> Issues { get; set; }
    public DbSet<Decision> Decisions { get; set; }
    public DbSet<RulingRequestRoutingInformation> RulingRequestRoutingInformations { get; set; }
    public DbSet<Staff> Staffs { get; set; }
    public DbSet<StatusOfRulingRequest> StatusOfRulingRequests { get; set; }

Using Package Manager, here are 3 steps I'm following:

NOTE: These are the generic instructions:

(1) enable-migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>
(2) Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>
(3) Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose

Step 2 is where I have my question. After I run Step 2, I get: a folder, called RulingRequestContextMigrations with 2 files:

201502212023508_Initial.cs and Configuration.cs.

When I look at Configuration.cs. file, the function, public override void Up(), has a CreateTable function for all 29 of my model classes.

I thought that only the 11 listed in my RulingRequestContext class would be created.

Here is a snapshot of my RulingRequest model class.

public class RulingRequest
{
    public RulingRequest()
    {
        this.RulingRequestResults = new HashSet<RulingRequestResult>();
        this.RulingRequestRoutingInformations = new HashSet<RulingRequestRoutingInformation>();
    }

    [DisplayName("Ruling Request ID")]
    public int RulingRequestID { get; set; }
    [DisplayName("Employee ID Number")]
    public string EmployeeIDNumber { get; set; }
    [DisplayName("Case Group")]
    public string RulingRequestGroupID { get; set; }
    [DisplayName("Type")]
    public Nullable<int> RulingRequestTypeID { get; set; }
    [DisplayName("First Name")]
    public string FirstName { get; set; }
    [DisplayName("Last Name")]
    public string LastName { get; set; }
    public string MI { get; set; }
    public string Suffix { get; set; }
    public string Address { get; set; }
    [DisplayName("Address 2")]
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public Nullable<int> Zip { get; set; }
    [DisplayName("Home Phone")]
    public string HomePhone { get; set; }
    [DisplayName("Work Phone")]
    public string WorkPhone { get; set; }
    public string Email { get; set; }
    [DisplayName("Agency")]
    public string AgencyNumber { get; set; }
    [DisplayName("Date Received")]
    public Nullable<System.DateTime> DateReceived { get; set; }
    [DisplayName("Grievance Initiation Date")]
    public Nullable<System.DateTime> GrievanceInitiationDate { get; set; }
    [DisplayName("Decision Date")]
    public Nullable<System.DateTime> DecisionDate { get; set; }
    [DisplayName("Re-Activation Date")]
    public Nullable<System.DateTime> ReActivationDate { get; set; }
    [DisplayName("Overall Outcome")]
    public Nullable<int> RulingRequestOverallOutcomeID { get; set; }
    [ScaffoldColumn(false)]
    public string CreatedBy { get; set; }
    [ScaffoldColumn(false)]
    public Nullable<System.DateTime> CreatedDatetime { get; set; }
    [ScaffoldColumn(false)]
    public string UpdatedBy { get; set; }
    [ScaffoldColumn(false)]
    public Nullable<System.DateTime> UpdatedDatetime { get; set; }

    public virtual Agency Agency { get; set; }
    public virtual RulingRequestGroup RulingRequestGroup { get; set; }
    public virtual RulingRequestOverallOutcome RulingRequestOverallOutcome { get; set; }
    public virtual RulingRequestType RulingRequestType { get; set; }
    public virtual ICollection<RulingRequestResult> RulingRequestResults { get; set; }
    public virtual ICollection<RulingRequestRoutingInformation> RulingRequestRoutingInformations { get; set; }

}

All 4 of my Data Context classes have a model class that uses Agency and has:

public virtual Agency Agency { get; set; }

QUESTIONS:

(1) Are all 29 of my model classes being added to the RulingRequest's Configuration.cs because of the relationships/virtual properties? For example since all 4 of my Data Context classes have: public virtual Agency Agency { get; set; } all of those model classes are being added to the Configuration.cs file for my RulingRequestContext.cs class.

(2) Assuming my initial Data Context class (RulingRequestContext) and it's Configuration.cs class are working as they are supposed to, should I comment out all of the CreateTable code in the 3 other Configuration.cs classes?

(3) Or should I comment out the CreateTable code for all of the model classes that don't appear as DBSet properties in my RulingRequestContext? And then do the same for my 3 other DataContext classes?

Thanks for your time reading this! Please let me know if there's anything I can do or not do to make my posts better in the future!

Yes, it is grabbing the related entities. Your link talks about commenting out the tables from the other contexts "Before running update command, commented out the generated code for Users tables as shown above. Since Users table is already created by first DbContext migrations."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM