【问题标题】:LINQ to SQL Inheritance InvalidOperationExceptionLINQ to SQL 继承 InvalidOperationException
【发布时间】:2013-04-23 13:56:53
【问题描述】:

尝试了解 LINQ 和继承映射时,执行 connectionstring 行时,我在 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;
        }
    }
}

我感觉我缺少一些讨厌的映射属性。

【问题讨论】:

    标签: .net linq orm


    【解决方案1】:

    好像没注意LINQ的single table inheritance。我删除了所有,但:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      • 2010-09-09
      • 2012-03-24
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多