2013-04-23 80 views
0

试图让我的周围LINQ和继承映射我得到DBContext.cs异常头:的LINQ to SQL继承InvalidOperationException异常

Could not retrieve Table for inheritance subtype 'Movie, try Table of 'Media' instead

DBContext.cs

​​

Media.cs

[Table(Name="media")] 
[InheritanceMapping(Code = 1, Type = typeof(Movie), IsDefault=true)] 
[InheritanceMapping(Code = 2, Type = typeof(Game))] 
[InheritanceMapping(Code = 3, Type = typeof(Album))] 
[InheritanceMapping(Code = 4, Type = typeof(Track))] 
public class Media 
{ 
    [Column(IsPrimaryKey=true)] 
    public string itemId; 

    [Column] 
    public string title { get; set; } 

    [Column] 
    public string price { get; set; } 

    [Column(IsDiscriminator=true)] 
    public int itemType { get; set; } 
} 

Movie.cs

public class Movie : Media 
{ 
    [Column] 
    public int year { get; set; } 
} 

而这里的代码中调用了一切:

public static void GetAll() 
    { 
     using (DBContext db = new DBContext()) 
     { 
      IEnumerable<Media> media = from m in db.media select m; 
     } 
    } 
} 

我有一种感觉,我错过了一些讨厌的映射属性。

回答

0

看来我没有注意到LINQ的single table inheritance。我删除所有,但:

public Table<Member> members; 
public Table<Media> media; 

现在它的工作。我将每个媒体项目放在自己的表格中,这是不受支持的。