Oracle
 sql >> Baza danych >  >> RDS >> Oracle

Mapowanie klucza obcego z niestandardową nazwą kolumny

Jeśli nie chcesz używać płynnej składni, istnieją trzy inne sposoby implementacji odniesienia za pomocą adnotacji danych (osobiście wolę adnotacje danych, ponieważ wydają się łatwiejsze do odczytania i są napisane tuż nad właściwością, na którą wpływają):

1.1) Użyj klucza obcego (z powiązaną właściwością) - wersja 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) Użyj klucza obcego (z powiązaną właściwością) - wersja 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)Możesz również użyć atrybutu 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; }
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ORA-27154 / ORA-27146

  2. Jak uzyskać typ danych kolumny w Oracle z PL-SQL z niskimi uprawnieniami?

  3. Jak zgłosić wyjątek w PL/SQL?

  4. Jak zmodyfikować typ danych w Oracle z istniejącymi wierszami w tabeli?

  5. Jak tworzyć tablice asocjacyjne w bazie danych Oracle