请接受我对大量文字的道歉,我尽量不要错过可能影响某些内容的细节 实体框架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]
为什么呢?我不明白我做错了什么... 有人可以帮忙吗? 谢谢!
答案 0 :(得分: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; }
}