0

请接受我对大量文本的道歉,我尽量不要错过可能影响实体框架 4.3.1 我的模型的细节:

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]

为什么?我不明白我做错了什么......有人可以帮忙吗?谢谢!

4

1 回答 1

0

当 EF 在生成数据库时将名称复数时,这看起来像一个问题 - 我前段时间遇到过这样的事情,但我忘记了哪个 EF 版本。

您可以使用以下命令强制 EF 命名表(尽管您可能需要删除/重新创建数据库):

[Table("PodcastStatus")]
public class PodcastStatus
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Podcast> Podcasts { get; set; }
} 
于 2012-05-18T14:19:21.083 回答