问题
I have a simple edmx
with 2 tables. The tables are related by a single Navigation Property
. (1 to many).
When I run my code I get an Exception: "Invalid Object Name dbo.Enquiries"
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?
Edited to show code as requested.
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();
Here is the sql that is generated.
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.
回答1:
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>();
}
回答2:
Try to remove the table from the edmx and after update the model.
回答3:
It seems that you are employing Model based development. If this is the case, then you seem to make some changes in the model, but did not reflect them to database.
Use the option generate database from model, and it will synchronize the database with the model.
回答4:
Try closing the Graphical view to EF model.
The right click edit and open in XML editor.
The mapping to table should be there and you can correct it.
OR
If you have chnaged the mode since import/Update, You could Completely delete the model and Import the Model again.
Code that accessed the model should be still Ok.
来源:https://stackoverflow.com/questions/14647951/ef-fault-when-retrieving-data-wrong-table-name-in-sql