【问题标题】:Confusion on Creating New Table EF Code-First One to Many创建新表 EF Code-First 一对多的困惑
【发布时间】:2018-01-24 17:15:11
【问题描述】:

我正在创建一个 EF Code-First MVC 应用程序。当我第一次开始时,我首先使用我知道我需要的表来创建数据库。现在我遇到了需要添加另一个表的情况。过去我通常使用 EF Database-First 并在 SSMS 中创建表并更新我的 EDMX,但我想扩展我的知识并开始执行 Code-First 项目。

所以我需要创建的表称为Event,该表将链接到另一个已经创建的表..称为ApplicantInformation

关系是1个事件可以有很多申请人信息-Event 1 ... * ApplicantInformation

这是我创建 Event 类的方法:

public class Event
{
    [Key]
    public int Id { get; set; }
    public string EventName { get; set; }
    public bool Active { get; set; }
}

这是我的ApplicantInformation 课程:

[Table("ApplicantInformation")]
public partial class ApplicantInformation
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public ApplicantInformation()
    {
        Events = new HashSet<Event>();
    }
    public int ID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int City { get; set; }

    public int State { get; set; }
    public string EmailAddress { get; set; }

    public bool Active { get; set; }
    public DateTime DateEntered { get; set; }
    public int EventId { get; set; }

    [ForeignKey("EventId")]
    public Event Event { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Event> Events { get; set; }
}

当我add-migration 我看到这个:

    public override void Up()
    {
        CreateTable(
            "dbo.Events",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    EventName = c.String(),
                    Active = c.Boolean(nullable: false),
                    ApplicantInformation_ID = c.Int(), // where does this come from?
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.ApplicantInformation", t => t.ApplicantInformation_ID) // where did ApplicantInformation_ID come from?
            .Index(t => t.ApplicantInformation_ID); // again, where did this property come from?

        AddColumn("dbo.ApplicantInformation", "EventId", c => c.Int(nullable: false));
        CreateIndex("dbo.ApplicantInformation", "EventId");
        AddForeignKey("dbo.ApplicantInformation", "EventId", "dbo.Events", "Id", cascadeDelete: true);
        DropColumn("dbo.ApplicantInformation", "CareerEvent");
    }

我的问题(如果您没有阅读 cmets),ApplicantInformation_ID 是从哪里来的?我显然没有将该属性放在我的 Event 类中?

感谢任何帮助。

更新

所以我可以摆脱ICollection&lt;Event&gt;ApplicantInformation 类中的构造函数,它仍然是一对多的关系吗?

public class Event
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Event()
    {
        ApplicantInformations = new HashSet<ApplicantInformation>();
    }

    [Key]
    public int Id { get; set; }
    public string EventName { get; set; }
    public bool Active { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ApplicantInformation> ApplicantInformations { get; set; }
}

[Table("ApplicantInformation")]
public partial class ApplicantInformation
{
    public int ID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int City { get; set; }

    public int State { get; set; }
    public string EmailAddress { get; set; }

    public bool Active { get; set; }
    public DateTime DateEntered { get; set; }
    public int EventId { get; set; }

    [ForeignKey("EventId")]
    public Event Event { get; set; }
}

迁移:

    public override void Up()
    {
        CreateTable(
            "dbo.Events",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    EventName = c.String(),
                    Active = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id);

        AddColumn("dbo.ApplicantInformation", "EventId", c => c.Int(nullable: false));
        CreateIndex("dbo.ApplicantInformation", "EventId");
        AddForeignKey("dbo.ApplicantInformation", "EventId", "dbo.Events", "Id", cascadeDelete: true);
        DropColumn("dbo.ApplicantInformation", "CareerEvent");
    }

【问题讨论】:

  • 它(按照惯例)来自ApplicantInformation 实体的public virtual ICollection&lt;Event&gt; Events { get; set; } 导航属性。有什么理由在那里添加该属性?因为它定义了另一个关系——ApplicantInformation 1 ... * Event
  • @IvanStoev 我添加的原因是因为我有最初在数据库中创建的其他表,然后当我选择使用 EF Code-First 时,然后将表带过来......我有使用该行的类.. 例如.. 在我的其他类之一中我有public virtual ICollection&lt;ApplicantInformation&gt; ApplicantInformations { get; set; },因为这个其他表与申请人信息表相关
  • 查看我的更新以了解我已更正的内容
  • 我明白了,但是你可能把错误的收藏放在了错误的地方。规则很简单 - 您将集合导航属性放在 one 一侧,并在 many 一侧引用导航属性。按照您描述所需关系的方式,集合应该是 Event 类中的 public ICollection&lt;ApplicantInformation&gt; Applicants { get; set; }
  • @IvanStoev 哦,天哪……你说得对……我确实把收藏放在了错误的地方……所以我编辑了我的问题,更新了我的班级现在的样子。看起来好吗?

标签: c# entity-framework asp.net-mvc-5 ef-code-first entity-framework-migrations


【解决方案1】:

评论部分中的每个 Ivan:

我明白了,但是你可能把错误的收藏放在了错误的地方。规则很简单 - 您将集合导航属性放在一侧,并在多侧引用导航属性。您描述所需关系的方式,集合应该是公共ICollection申请人{get;放; } 在 Event 类中。

这正是我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多