Mapping a foreign key with a custom column name

前端 未结 3 1740
情书的邮戳
情书的邮戳 2020-12-05 03:51

I\'m using Entity Framework 4.3 code-first with Oracle. I\'m getting the following error:

System.InvalidOperationException : The ForeignKeyAttribute o

相关标签:
3条回答
  • 2020-12-05 04:28

    If you don't want to use fluent syntax, there are three other ways of implementing the reference using data annotations (Personally I prefer data annotations as they seem easier to read and are written just above the property they are affecting):

    1.1) Use ForeignKey (with an associated property) - version 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) Use ForeignKey (with an associated property) - version 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) You can also use the 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 讨论(0)
  • 2020-12-05 04:41

    ForeignKey attibute expects a property name in your class as the argument but you given the column name. Use fluent mappings.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    
        modelBuilder.Entity<WidgetEntity>()
         .HasRequired(w => w.Sequence)
         .WithMany()
         .Map(m => m.MapKey("WIDGETSEQUENCE_ID"));
    }
    
    0 讨论(0)
  • 2020-12-05 04:41

    There is a table called Users and it has a primary key called UserID.

    There is another table called Directory, and it has a column called UserID which is defined as a foreign key to the Users table.

    I'm able to use the ForeignKey annotation to map the foreign key like this:

    [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
    
    0 讨论(0)
提交回复
热议问题