2013-01-22 89 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