EF检索数据时出错:SQL中的表名错误 [英] EF Fault when retrieving data: wrong table name in SQL
问题描述
我有一个简单的 edmx
与2表。表格由单一的导航属性
相关联。 (1到许多)
I have a simple edmx
with 2 tables. The tables are related by a single Navigation Property
. (1 to many).
当我运行我的代码时,我得到一个异常:无效的对象名称dbo.Enquiries
When I run my code I get an Exception: "Invalid Object Name dbo.Enquiries"
数据库中没有 dbo.Enquiries
(实际上称为 dbo.Enquiry
),所以错误本身就是自解释的。
There is no dbo.Enquiries
in the database (it is actually called dbo.Enquiry
), so the error itself is self explanatory. But where is it finding that Name, and how do I fix it?
编辑为按要求显示代码。 >
var foo = (from d in context.Dealerships
join e in context.Enquiry
on d.Id equals e.DealershipId
where (d.ParentBusinessId == id)
select d).AsEnumerable();
这是生成的sql。
foo {SELECT
[Extent1].[Id] AS [Id],
[Extent1].[BusinessName] AS [BusinessName]
FROM [dbo].[Dealerships] AS [Extent1]
INNER JOIN [dbo].[Enquiries] AS [Extent2] ON [Extent1].[Id] =
[Extent2].[DealershipId]
WHERE [Extent1].[ParentBusinessId] = @p__linq__0}
但是对于我的生活不能看到哪里/如何决定更改名称查询内部联接的查询。
But for the life of me I can't see where / how it is deciding to change the name Enquiry to Enquiries on the inner join.
推荐答案
找到答案。多数只是在模型一代。我不得不明确地告诉我的DbContext不要复制名字(我不敢开始重命名这个数据库,所以我卡住了所使用的奇怪的约定。)
Found the Answer. the Pluralise Is only on Model Generation. I had to explicitly tell my DbContext not to Pluralise the names (I dare not start to rename this database, so Im stuck with the weird convention that was used.)
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
这篇关于EF检索数据时出错:SQL中的表名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!