简体   繁体   English

为什么我的所有模型类都被添加到我使用多个数据上下文类时创建的第一个Configuration.cs文件中?

[英]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 实体框架6具有多个数据上下文的代码优先迁移

NOTE: This is my first post on Stack Overflow so please let me know if I need to provide more detail. 注意:这是我在Stack Overflow上的第一篇文章,所以如果我需要提供更多细节,请告诉我。

FYI: I have read the article: 仅供参考:我读过这篇文章:

Entity Framework 6 Code First Migrations with Multiple Data Contexts 实体框架6具有多个数据上下文的代码优先迁移

And I'm trying to do something similar. 而我正在尝试做类似的事情。 My question/problem revolves around the CreateTable methods generated in the Configuration.cs file. 我的问题/问题围绕Configuration.cs文件中生成的CreateTable方法。

I have 29 model classes, and I'm trying to set up 4 different Data Contexts. 我有29个模型类,我正在尝试设置4个不同的数据上下文。

In my first Data Context, RulingRequestContext, I have 11 of my 29 model classes: 在我的第一个数据上下文RulingRequestContext中,我有29个模型类中的11个:

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: 使用包管理器,这是我正在遵循的3个步骤:

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. 第2步是我的问题所在。 After I run Step 2, I get: a folder, called RulingRequestContextMigrations with 2 files: 在我运行第2步之后,我得到:一个名为RulingRequestContextMigrations的文件夹,包含2个文件:

201502212023508_Initial.cs and Configuration.cs. 201502212023508_Initial.cs和Configuration.cs。

When I look at Configuration.cs. 当我查看Configuration.cs时。 file, the function, public override void Up(), has a CreateTable function for all 29 of my model classes. file,函数,public override void Up(),为我的所有29个模型类都有一个CreateTable函数。

I thought that only the 11 listed in my RulingRequestContext class would be created. 我认为只会创建我的RulingRequestContext类中列出的11个。

Here is a snapshot of my RulingRequest model class. 这是我的RulingRequest模型类的快照。

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: 我的所有4个数据上下文类都有一个使用Agency的模型类,并具有:

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? (1)由于关系/虚拟属性,是否所有29个模型类都被添加到RulingRequest的Configuration.cs中? For example since all 4 of my Data Context classes have: public virtual Agency Agency { get; 例如,因为我的所有4个数据上下文类都具有:public virtual Agency Agency {get; set; 组; } all of those model classes are being added to the Configuration.cs file for my RulingRequestContext.cs class. 所有这些模型类都被添加到我的RulingRequestContext.cs类的Configuration.cs文件中。

(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? (2)假设我的初始数据上下文类(RulingRequestContext)和它的Configuration.cs类正在按预期工作,我是否应该注释掉其他3个Configuration.cs类中的所有CreateTable代码?

(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? (3)或者我应该为我的RulingRequestContext中未显示为DBSet属性的所有模型类注释掉CreateTable代码? And then do the same for my 3 other DataContext classes? 然后为我的其他3个DataContext类做同样的事情?

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." 您的链接讨论了从其他上下文中注释表“在运行更新命令之前,如上所示注释了用户表的生成代码。由于Users表已经由第一次DbContext迁移创建。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我选择从数据库更新.edmx文件时,为什么Model.tt类不会得到更新 - why the Model.tt classes will not get updated when i chose to update my .edmx file from database 实体框架-首先在模型中,如何使我的EF类基于我已有的现有域类? - Entity Framework - in model first, how do I get my EF classes to be based on the existing domain classes I have? 实体框架代码优先:Configuration.cs种子或自定义初始化程序 - Entity Framework Code First: Configuration.cs seed or custom initializer 如何在代码优先迁移中解决Configuration.cs中的错误 - How to solve error in Configuration.cs in code first migration 如何从EF Configuration.cs文件读取文件内容? - How to read a file content from EF Configuration.cs file? 在使用Model-First方法时,如何将XML注释添加到Entity Framework生成的自动生成的类中? - How do I add XML comments to the auto-generated classes produced by Entity Framework when using Model-First approach? 添加“使用实体框架的 controller”时未列出 Model 类 - Model classes are not listed when "a controller using Entity Framework" is added Entity Framework Core Enable-Migrations 构建带有错误的 Configuration.cs 文件 - Entity Framework Core Enable-Migrations build the Configuration.cs file with Errors 为什么我只使用1个上下文,但是我的代码却触发了多个数据库上下文 - Why is my code triggering multiple DB context though I'm only using 1 context 如何将我的模型类与MVC 3中的现有数据库绑定? - How do I tie my Model classes with an existing database in MVC 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM