首页 文章

使用自定义列名映射外键

提问于
浏览
39

我在Oracle中使用Entity Framework 4.3代码优先 . 我收到以下错误:

System.InvalidOperationException:类型为“WidgetDistributor.WidgetEntity”的属性“WidgetSequence”上的ForeignKeyAttribute无效 . 在依赖类型“WidgetDistributor.WidgetEntity”上找不到外键名称“WIDGETSEQUENCE_ID” . Name值应该是以逗号分隔的外键属性名称列表 .

我的实体是这样的:

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

我的代码似乎正确 . 我做错了什么,这里?

3 回答

  • 56

    ForeignKey attibute期望您的类中的属性名称作为参数,但您给出了列名称 . 使用流畅的映射 .

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    
        modelBuilder.Entity<WidgetEntity>()
         .HasRequired(w => w.Sequence)
         .WithMany()
         .Map(m => m.MapKey("WIDGETSEQUENCE_ID"));
    }
    
  • 1

    如果您不想使用流畅的语法,还有其他三种使用数据注释实现引用的方法(我个人更喜欢数据注释,因为它们看起来更容易阅读并且只是在它们正在影响的属性之上编写):

    1.1)使用ForeignKey(具有关联属性) - 版本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)使用ForeignKey(具有关联属性) - 版本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)您也可以使用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; }
    }
    
  • 34

    有一个名为Users的表,它有一个名为UserID的主键 .

    还有另一个名为Directory的表,它有一个名为UserID的列,它被定义为Users表的外键 .

    我可以使用ForeignKey批注来映射外键,如下所示:

    [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
    

相关问题