0

LINQ と継承マッピングに頭を悩ませようとすると、接続文字列の行が実行されると、DBContext.cs で例外が発生します。

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

DBContext.cs

[Database]
public class DBContext : DataContext
{
    public DBContext() : base(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString){ }

    public Table<Member> members;
    public Table<Media> media;
    public Table<Movie> movies;
    public Table<Game> games;
    public Table<Track> tracks;
    public Table<Album> albums;
  //  public Table<Loaned> loaned;
}

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;
        }
    }
}

厄介なマッピング属性が欠けているような気がします。

4

1 に答える 1