0

Je suis en train de configurer une relation simple en option nécessaire, mais EF ne semble pas échafaudage la migration droite:EF API Courant One-to-One relation

public class Applicant 
{ 
    public int Id { get; set; } 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public int ContactInfoId   { get; set; } 
    public string PreferredCultureId { get; set; } 

    public virtual ApplicantContact  Contact    { get; set; } 
    public virtual Culture    PreferredCulture { get; set; } 
} 




public ApplicantConfiguration() 
{ 
    HasKey(a => a.Id); 
    Property(a => a.FirstName).IsRequired().HasMaxLength(50); 
    Property(a => a.LastName).IsRequired().HasMaxLength(50); 

    HasOptional(a => a.Contact) 
     .WithRequired(c => c.Applicant) 
     .WillCascadeOnDelete(); 

    HasOptional(a => a.PreferredCulture) 
     .WithMany() 
     .HasForeignKey(a => a.PreferredCultureId); 
} 






CreateTable(
    "Main.Applicants", 
    c => new 
     { 
      Id = c.Int(nullable: false, identity: true), 
      FirstName = c.String(nullable: false, maxLength: 50), 
      LastName = c.String(nullable: false, maxLength: 50), 
      ContactInfoId = c.Int(nullable: false), 
      PreferredCultureId = c.String(maxLength: 128), 
     }) 
    .PrimaryKey(t => t.Id) 
    .ForeignKey("General._Cultures", t => t.PreferredCultureId) 
    .Index(t => t.PreferredCultureId); 

Pourquoi ContactInfoId est de ne pas généré comme une clé étrangère et nullable, car il est le côté facultatif de la relation?

Répondre

0

Dans votre classe de domaine essayer

public class Applicant 
{ 

    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public virtual ApplicantContact  Contact    { get; set; } 
    public virtual Culture    PreferredCulture { get; set; } 
} 


public class ContactInfo 
{ 
    // whatever contact info fields you have 
} 

public class Culture 
{ 
    // culture fields 
} 

alors dans votre contexte ont

public DbSet<ContactInfo> ContactInfos { get; set; } 
public DbSet<Applicant> Applicants { get; set; } 
public DbSet<Culture> Cultures { get; set; } 

Les champs Id devraient obtenir automatiquement si elles sont int.

Questions connexes