使用自定义列名映射外键

时间:2012-06-22 00:18:31

标签: c# oracle ef-code-first entity-framework-4.3

我在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 个答案:

答案 0 :(得分:56)

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

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

答案 1 :(得分:34)

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

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

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

答案 2 :(得分:1)

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

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

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

<mat-checkbox class = "example-margin" [(ngModel)] = obj.impresora> 
     <label>Printer</label> 
</mat-checkbox>