我正在使用带有 SQL Server 2008 R2 的 VS2010。
我是 c# 和实体框架的完整初学者。
我在下面的示例中遇到的问题是,当我尝试获取与特定选项卡关联的问题时,生成的 SQL 不正确。我认为这与我在 2 个引用同名字段的表上有一个 Tab_ID 外键这一事实有关吗?
但是,当我尝试获取与 Checksheets 关联的选项卡时,生成的 SQL 是正确的。我所做的一切都完全相同,但产生了不同的结果?
任何帮助将不胜感激。
下面我附上了使用的方法和输出以及我使用的数据库关系模型和映射器。
与 CheckSheets 关联的选项卡(按预期工作)
public IQueryable<CheckSheetTab> getTabs(int checkSheetId)
{
//includes the foreign key information when returning Tab
var allReturnInfo = CheckSheetTab.Include(x => x.Tab);
var result = allReturnInfo.Where(p => p.CheckSheet_ID == checkSheetId);
result = result.OrderBy(x => x.Order_ID);
return result;
}
生成的 SQL
{SELECT
[Extent1].[Table_ID] AS [Table_ID],
[Extent1].[Tab_ID] AS [Tab_ID],
[Extent1].[CheckSheet_ID] AS [CheckSheet_ID],
[Extent1].[Order_ID] AS [Order_ID],
[Extent2].[Tab_ID] AS [Tab_ID1],
[Extent2].[Tab_Desc] AS [Tab_Desc]
FROM [dbo].[tbl_CheckSheet_Tabs] AS [Extent1]
INNER JOIN [dbo].[tbl_Tabs] AS [Extent2] ON [Extent1].[Tab_ID] = [Extent2].[Tab_ID]}
与标签相关的问题(不起作用)
public IQueryable<TabQuestion> getQuestions(int tabId)
{
//includes the foreign key information when returning Tab
var allReturnInfo = TabQuestion.Include(x => x.Question);
// NOTE FOR SOME REASON THIS IS NOT CREATING THE CORRECT SQL!!!!!!!!!
var result = allReturnInfo.Where(p => p.Tab_ID == tabId);
result = result.OrderBy(x => x.Order_ID);
return result;
}
SQL PRODUCED - EXTENT 3 是问题,我不明白为什么会产生它。
{SELECT
[Extent1].[Table_ID] AS [Table_ID],
[Extent1].[Question_ID] AS [Question_ID],
[Extent1].[Tab_ID] AS [Tab_ID],
[Extent1].[Order_ID] AS [Order_ID],
[Extent2].[Question_ID] AS [Question_ID1],
[Extent2].[Question_Desc] AS [Question_Desc],
[Extent2].[Parent_ID] AS [Parent_ID],
[Extent3].[Tab_Tab_ID] AS [Tab_Tab_ID]
FROM [dbo].[tbl_Tab_Questions] AS [Extent1]
INNER JOIN [dbo].[tbl_Questions] AS [Extent2] ON [Extent1].[Question_ID] = [Extent2].[Question_ID]
LEFT OUTER JOIN [dbo].[tbl_Questions] AS [Extent3] ON [Extent1].[Question_ID] = [Extent3].[Question_ID]}
我创建了以下数据库:(我还没有 10 个发布图片的声誉,但链接如下) http://imgur.com/3Jchhlb
我为表设置了映射器,如下所示:
class CheckSheetTabConfiguration : EntityTypeConfiguration<CheckSheetTab>
{
public CheckSheetTabConfiguration() : base()
{
HasKey(x => x.Table_ID);
Property(p => p.Tab_ID)
.HasColumnName("Tab_ID");
Property(p => p.CheckSheet_ID)
.HasColumnName("CheckSheet_ID");
Property(p => p.Order_ID)
.HasColumnName("Order_ID");
//add the foreign key constraint
HasRequired(p => p.Tab)
.WithMany()
.HasForeignKey(x => x.Tab_ID);
HasRequired(p => p.CheckSheet)
.WithMany()
.HasForeignKey(x => x.CheckSheet_ID);
ToTable("tbl_CheckSheet_Tabs");
}
}
class TabQuestionConfiguration : EntityTypeConfiguration<TabQuestion>
{
public TabQuestionConfiguration()
: base()
{
HasKey(x => x.Table_ID);
Property(p => p.Tab_ID)
.HasColumnName("Tab_ID");
Property(p => p.Question_ID)
.HasColumnName("Question_ID");
Property(p => p.Order_ID)
.HasColumnName("Order_ID");
//add the foreign key constraint
HasRequired(p => p.Question)
.WithMany()
.HasForeignKey(x => x.Question_ID);
HasRequired(p => p.Tab)
.WithMany()
.HasForeignKey(x => x.Tab_ID);
ToTable("tbl_Tab_Questions");
}
}
我的课程设置如下:
public class CheckSheetTab
{
public Int32 Table_ID { get; set; }
public Int32 Tab_ID { get; set; }
public Int32 CheckSheet_ID { get; set; }
public Int32 Order_ID { get; set; }
public virtual Tab Tab { get; set; }
public virtual CheckSheet CheckSheet { get; set; }
}
public class TabQuestion
{
public Int32 Table_ID { get; set; }
public Int32 Question_ID { get; set; }
public Int32 Tab_ID { get; set; }
public Int32 Order_ID { get; set; }
public virtual Question Question { get; set; }
public virtual Tab Tab { get; set; }
}
*标签配置
class TabConfiguration : EntityTypeConfiguration<Tab>
{
public TabConfiguration() : base()
{
HasKey(x => x.Tab_ID);
Property(p => p.Tab_Desc)
.HasColumnName("Tab_Desc");
ToTable("tbl_Tabs");
}
}
*问题配置
class QuestionConfiguration : EntityTypeConfiguration<Question>
{
public QuestionConfiguration() : base()
{
HasKey(x => x.Question_ID);
Property(p => p.Question_Desc)
.HasColumnName("Question_Desc");
Property(p => p.Parent_ID)
.HasColumnName("Parent_ID");
ToTable("tbl_Questions");
}
}
*检查表配置
class CheckSheetConfiguration : EntityTypeConfiguration<CheckSheet>
{
public CheckSheetConfiguration() : base()
{
HasKey(x => x.CheckSheet_ID);
Property(p => p.CheckSheet_Desc)
.HasColumnName("CheckSheet_Desc");
Property(p => p.Tab_Count)
.HasColumnName("Tab_Count");
ToTable("tbl_CheckSheets");
}
}
基类
public class CheckSheet
{
public int CheckSheet_ID { get; set; }
public String CheckSheet_Desc { get; set; }
public int Tab_Count { get; set; }
public List<Tab> tabs { get; set; }
}
public class Tab
{
public int Tab_ID { get; set; }
public string Tab_Desc { get; set; }
public List<Question> questions { get; set; }
}
public class Question
{
public int Question_ID { get; set; }
public int Question_Desc { get; set; }
public int Parent_ID { get; set; }
}