0

Я пытаюсь сопоставить эти объекты:EF5 отображение Fluent API

[Table("AAN_DigitalLib_Asset")] 
    public class Asset 
    { 
    [Column("ID")] 
    public int ID { get; set; } 

    [Column("format_id")] 
    public int FormatID { get; set; } 

    [Column("person_id")] 
    public int? PersonID { get; set; } 

    [Column("publicationYear")] 
    public int? PublicationYear { get; set; } 

    public Person People { get; set; } 

    //public virtual AssetKeyword AssetKeywords { get; set; } 

    public virtual ICollection<AssetKeyword> AssetKeywords { get; set; } 
} 

С этими объектами:

[Table("AAN_DigitalLib_AssetKeyword")] 
    public class AssetKeyword 
    { 
     [Column("ID")] 
     public int ID { get; set; } 

     [Column("asset_id")] 
     public int AssetID { get; set; } 

     [Column("keyword")] 
     public string Keyword { get; set; } 
    } 

Где один Asset может иметь много AssetKeywords. В базе данных нет отношения, так как я могу создать ее с помощью Fluent API?

ответ

1

ли добавление

[ForeignKey("AssetID")] 
public virtual Asset Asset { get; set; } 

на классе AssetKeyword это исправить?

Если это не сделать, то сделать это в onModelCreating()

modelBuilder.Entity<AssetKeyword>() 
        .HasRequired(p => p.Asset) 
        .WithMany() 
        .HasForeignKey(p => p.AssetID); 
+0

Спасибо, я думаю, ваш первый пример сделал трюк. – user547794

Смежные вопросы