2013-01-22 118 views
1

外鍵我有點使用實體框架5.我創造了我的實體兩個接口混淆字:實體框架從接口

public class Word : IWord 
{ 
    [Key] 
    [Required] 
    public int WordId { get; set; } 

    [Required] 
    public string Tag { get; set; } 

    [Required] 
    public string Translation { get; set; } 

    [Required] 
    public char Language { get; set; } 

    public string Abbreviation { get; set; } 

    //Foreign Key 
    public int VocabularyId { get; set; } 

    //Navigation 
    public virtual IVocabulary Vocabulary { get; set; } 
} 

詞彙:

public class Vocabulary : IVocabulary 
{ 
    [Key] 
    [Required] 
    public int VocabularyId { get; set; } 

    [Required] 
    public string Name { get; set; } 

    public virtual List<IWord> Words { get; set; } 
} 

,並在結束時,我DataContext我寫道:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Word>() 
    .HasRequired(w => w.Vocabulary) 
    .WithMany(v => v.Words) 
    .HasForeignKey(w => w.VocabularyId) 
    .WillCascadeOnDelete(false); 

    base.OnModelCreating(modelBuilder); 
} 

而且我得到這個錯誤:

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.ICollection'. An explicit conversion exists (are you missing a cast?)

我試圖刪除接口,一切都很好..

有幫助嗎?

謝謝

回答

3

實體框架無法處理導航屬性中的接口。它不知道如何實現它們。所以你可以保留接口類型(class Word : IWord等),但Vocabulary.Words應該是ICollection<Word>

+0

我明白了。謝謝! – Davide