2012-06-22 8 views
29

Sto utilizzando Entity Framework 4.3 code-first con Oracle. Sto ottenendo il seguente errore:Mapping di una chiave esterna con un nome di colonna personalizzato

System.InvalidOperationException : The ForeignKeyAttribute on property 'WidgetSequence' on type 'WidgetDistributor.WidgetEntity' is not valid. The foreign key name 'WIDGETSEQUENCE_ID' was not found on the dependent type 'WidgetDistributor.WidgetEntity'. The Name value should be a comma separated list of foreign key property names.

I miei soggetti sono come questo:

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [ForeignKey("WIDGETSEQUENCE_ID")] 
    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

Il mio codice sembra corretto. Cosa ho fatto di sbagliato, qui?

risposta

28

ForeignKey attibute si aspetta un nome di proprietà nella classe come argomento, ma è dato il nome di colonna. Utilizza mappature fluenti.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 

    modelBuilder.Entity<WidgetEntity>() 
    .HasRequired(w => w.Sequence) 
    .WithMany() 
    .Map(m => m.MapKey("WIDGETSEQUENCE_ID")); 
} 
+0

Grazie per la risposta. Che dire di questo problema? Potresti dare un'occhiata per favore? Grazie in anticipo ... http://stackoverflow.com/questions/29333787/how-to-create-lookup-table-and-define-relationships –

47

Se non si desidera utilizzare la sintassi fluente, ci sono altri tre modi di attuazione del riferimento utilizzando le annotazioni di dati (personalmente preferisco le annotazioni di dati come sembrano più facili da leggere e sono scritti appena sopra la proprietà che stanno interessando):

1,1) Usa ForeignKey (con una proprietà associata) - versione 1

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [Column("WIDGETSEQUENCE_ID")] 
    public int WidgetSequenceId { get; set; } 

    [ForeignKey("WidgetSequenceId")] //Has to be a property name, not table column name 
    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

1,2) Uso ForeignKey (con una proprietà associata) - versione 2

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [ForeignKey("Sequence")] //Has to be a property name, not table column name 
    [Column("WIDGETSEQUENCE_ID")] 
    public int WidgetSequenceId { get; set; } 

    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 
} 

2) È inoltre possibile utilizzare l'InversePropertyAttribute.

[Table("WIDGETENTITIES")] 
public class WidgetEntity { 

    [Column("WIDGETENTITY_ID")] 
    public int Id { get; set; } 

    [InverseProperty("WidgetEntities")] 
    public WidgetSequence Sequence { get; set; } 

    // and other properties that map correctly 
} 

[Table("WIDGETSEQUENCES")] 
public class WidgetSequence { 

    [Column("WIDGETSEQUENCE_ID")] 
    public int Id { get; set; } 

    [Column("NUMBER")] 
    public int Number { get; set; } 

    public virtual List<WidgetEntity> WidgetEntities { get; set; } 
} 
+0

Grazie per questo! La documentazione e alcune altre risposte SO suggeriscono che l'attributo chiave esterna può essere utilizzato dove dovrebbe essere effettivamente la proprietà inversa. – Carl

+0

@Carl e quei suggerimenti che hai trovato non sono il modo corretto. L'attributo FK va sopra la colonna, non la proprietà inversa. – vapcguy

+0

@RichardPierre Grazie per la risposta. Tuttavia, non posso fare riferimento a un'altra colonna tranne dalla colonna Id con InverseProperty. Che dire di questo problema? Cosa c'è che non va? http://stackoverflow.com/questions/29327772/how-to-define-matching-column-in-inverseproperty?noredirect=1#comment46851150_29327772 –

0

C'è una tabella denominata Utenti e ha una chiave primaria denominata UserID.

C'è un'altra tabella denominata Directory e ha una colonna denominata UserID definita come chiave esterna per la tabella Users.

sono in grado di usare l'annotazione ForeignKey per mappare la chiave esterna in questo modo:

[ForeignKey("xyzzy")] 
public int? UserID { get; set; } // This is a column in the table 
public virtual User xyzzy { get; set; } // This is my instance of User 
Problemi correlati