大量のテキストについてお詫び申し上げます。Entity Framework 4.3.1 My Model に影響を与える可能性のある詳細を見逃さないようにしました。
public class Podcast
{
public int Id { get; set; }
public string Title { get; set; }
public int PodcastStatus_Id { get; set; }
public int PodcastType_Id { get; set; }
public virtual PodcastStatus PodcastStatus { get; set; }
public virtual PodcastType PodcastType { get; set; }
public virtual ICollection<CategoryLink> CategoryLinks { get; set; }
}
public class PodcastStatus
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Podcast> Podcasts { get; set; }
}
public class PodcastType
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Podcast> Podcasts { get; set; }
}
環境:
public class EcDbContext : DbContext
{
public DbSet<PodcastType> PodcastTypes { get; set; }
public DbSet<PodcastStatus> PodcastStatuses { get; set; }
public DbSet<Podcast> Podcasts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Podcast>().HasRequired(p => p.PodcastStatus).WithMany(s => s.Podcasts).HasForeignKey(p => p.PodcastStatus_Id);
modelBuilder.Entity<Podcast>().HasRequired(p => p.PodcastType).WithMany(t => t.Podcasts).HasForeignKey(p => p.PodcastType_Id);
base.OnModelCreating(modelBuilder);
}
}
クエリ:
db.Podcasts.Where(p => p.PodcastStatus.SysStatus > 0 && p.Title.Contains(search))
.OrderBy(p => p.CreatedDate)
.Skip((page - 1) * PageSize).Take(PageSize)
ここでは、複雑にならないように、モデルの説明からいくつかの列を削除しました。次のエラー メッセージが表示されますInvalid object name 'dbo.PodcastStatus'
。このテーブルは、データベースでは「PodcastStatuses」という名前です。結果の SQL クエリを見ると、次のテキストが表示されます。
{SELECT TOP (10)
[Project1].[Id] AS [Id],
[Project1].[Title] AS [Title],
[Project1].[CreatedDate] AS [CreatedDate],
[Project1].[PodcastStatus_Id] AS [PodcastStatus_Id],
[Project1].[PodcastType_Id] AS [PodcastType_Id],
[Project1].[PodcastStatus_Id1] AS [PodcastStatus_Id1],
[Project1].[PodcastType_Id1] AS [PodcastType_Id1]
FROM ( SELECT [Project1].[Id] AS [Id], [Project1].[CastNo] AS [CastNo], [Project1].[Title] AS [Title], [Project1].[OriginalText] AS [OriginalText], [Project1].[TranslateText] AS [TranslateText], [Project1].[CreatedDate] AS [CreatedDate], [Project1].[PodcastStatus_Id] AS [PodcastStatus_Id], [Project1].[PodcastType_Id] AS [PodcastType_Id], [Project1].[ImageFileName] AS [ImageFileName], [Project1].[PodcastStatus_Id1] AS [PodcastStatus_Id1], [Project1].[PodcastType_Id1] AS [PodcastType_Id1], row_number() OVER (ORDER BY [Project1].[CreatedDate] ASC) AS [row_number]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Title] AS [Title],
[Extent1].[CreatedDate] AS [CreatedDate],
[Extent1].[PodcastStatus_Id] AS [PodcastStatus_Id],
[Extent1].[PodcastType_Id] AS [PodcastType_Id],
[Extent1].[PodcastStatus_Id1] AS [PodcastStatus_Id1],
[Extent1].[PodcastType_Id1] AS [PodcastType_Id1]
FROM [dbo].[Podcasts] AS [Extent1]
INNER JOIN [dbo].[PodcastStatus] AS [Extent2] ON [Extent1].[PodcastStatus_Id] = [Extent2].[Id]
WHERE ([Extent2].[SysStatus] > 0) AND ((N'' = @p__linq__0) OR ([Extent1].[Title] LIKE @p__linq__1 ESCAPE N'~'))
) AS [Project1]
) AS [Project1]
WHERE [Project1].[row_number] > 0
ORDER BY [Project1].[CreatedDate] ASC}
PodcastStatus_Id1, PodcastStatus_Id1, [dbo].[PodcastStatus] (and not 'statusES'), [PodcastStatus_Id] = [Extent2].[Id]
なんで?何を間違えたのか理解できません...誰か助けてくれますか?ありがとう!