-1

Je crée une application EF Code-First MVC. Quand j'ai commencé, j'ai fait d'abord la base de données avec les tables dont je savais que j'aurais besoin. Maintenant, j'ai rencontré une situation où j'ai besoin d'ajouter une autre table. Dans le passé, je fais habituellement EF Database-First et je crée simplement la table dans SSMS et je mets à jour mon EDMX, mais je veux développer mes connaissances et commencer à faire des projets avec Code-First. Donc la table que j'ai besoin de créer s'appelle Event et cette table sera liée à une autre table déjà créée .. appelée ApplicantInformation.Confusion sur la création d'une nouvelle table Code EF - Premier à plusieurs

La relation est que 1 événement peut avoir de nombreuses informations sur le demandeur - Event 1 ... * ApplicantInformation.

Voici comment j'ai créé ma Event classe:

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

Voici ma ApplicantInformation classe:

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

Quand je add-migration je vois ceci:

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

Ma question (si vous n'avez pas lu les commentaires), est où ApplicantInformation_ID viennent? Je n'ai évidemment pas mis cette propriété dans ma classe Event?

Toute aide est appréciée.

MISE À JOUR

donc je ne peux tout simplement se débarrasser des ICollection<Event> et le constructeur dans ma ApplicantInformation classe et il sera toujours une relation un à plusieurs?

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

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), 
       }) 
      .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"); 
    } 
+0

Il vient (par convention) de 'public virtual ICollection Events {get; ensemble; } 'Propriété de navigation de l'entité' ApplicantInformation'. Une raison d'ajouter cette propriété là? Parce qu'il définit une autre relation - 'ApplicantInformation 1 ... * Event'. –

+0

@IvanStoev La raison pour laquelle j'ai ajouté cela était parce que j'ai d'abord créé d'autres tables dans la base de données puis quand j'ai choisi d'utiliser EF Code-First, et j'ai ramené les tables .. par exemple .. dans une de mes autres classes, j'ai 'public virtual ICollection ApplicantInformations {get; ensemble; } 'parce que cette autre table est liée à la table ApplicantInformations –

+0

voir ma mise à jour pour ce que j'ai corrigé –

Répondre

0

par Ivan dans la section commentaire:

Je comprends, mais vous avez probablement mis la mauvaise collection au mauvais endroit. La règle est simple: vous mettez la propriété de navigation de collection d'un côté et la propriété de navigation de référence de plusieurs côtés. La façon dont vous avez décrit la relation souhaitée, la collection doit être publique ICollection Applicants {get; ensemble; } dans la classe Event.

C'était exactement mon problème.