使用自定义列名映射外键

Byr*_*ahl 40 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; }
}
Run Code Online (Sandbox Code Playgroud)

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

xx1*_*1xx 61

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

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)


Era*_*nga 36

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

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

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