EF select没有获得记录

时间:2017-03-15 21:03:14

标签: c# sql-server entity-framework

我有一个大型项目,正在进行大量数据访问。我刚刚添加了一个新表(来自db),标准EF查询不起作用,但是sql查询有效。

这是数据访问:

public Topic GetTopicByName(string name) ... name value is KboInterestingTopic
{
    var topics = from t in _db.Topics
        where t.Name == name
        select t;

    return topics.FirstOrDefault();
}

这是有效的sql(使用与ef查询相同的密钥:

SELECT * FROM Topic
WHERE Name = 'KboInterestingTopic'

...看起来生成的选择是坏的,看看最后一行??

{SELECT 
[Extent1].[TopicID] AS [TopicID], 
[Extent1].[TopicGUID] AS [TopicGUID], 
[Extent1].[Name] AS [Name], 
[Extent1].[Title] AS [Title], 
[Extent1].[Description] AS [Description], 
[Extent1].[SETitle] AS [SETitle], 
[Extent1].[SEDescription] AS [SEDescription], 
[Extent1].[SEKeywords] AS [SEKeywords], 
[Extent1].[Password] AS [Password], 
[Extent1].[RequiresDisclaimer] AS [RequiresDisclaimer], 
[Extent1].[XmlPackage] AS [XmlPackage], 
[Extent1].[ExtensionData] AS [ExtensionData], 
[Extent1].[ShowInSiteMap] AS [ShowInSiteMap], 
[Extent1].[SkinID] AS [SkinID], 
[Extent1].[HTMLOk] AS [HTMLOk], 
[Extent1].[Deleted] AS [Deleted], 
[Extent1].[DisplayOrder] AS [DisplayOrder], 
[Extent1].[CreatedOn] AS [CreatedOn], 
[Extent1].[StoreID] AS [StoreID], 
[Extent1].[Published] AS [Published], 
[Extent1].[UpdatedOn] AS [UpdatedOn], 
[Extent1].[IsFrequent] AS [IsFrequent]
FROM [dbo].[Topic] AS [Extent1]
WHERE [Extent1].[Name] = @p__linq__0}

我看不到,@ p__linq__0 = N'KboInterestingTopic'

2 个答案:

答案 0 :(得分:0)

试试这个

return _db.Topics.FirstOrDefault(
    s => s.Name.Equals(name, StringComparison.OrdinalIgnoreCase)
);

答案 1 :(得分:0)

感觉非常愚蠢......

使用测试系统 - 指向没有测试数据的生产数据库。它按预期工作。

感谢您的所有回复。

雅培