Entity Framework (4.3) looking for singular name instead of plural (when entity name ends with “s”)

后端 未结 4 1937
半阙折子戏
半阙折子戏 2021-02-05 15:25

Here\'s my situation: I have been working on an ASP.NET MVC 3 application for a while. It has a database (built out of a db project; I\'m going db-first) for which I have an edm

4条回答
  •  死守一世寂寞
    2021-02-05 16:24

    Got it! Horrible, horrible experience...

    In short: EF cannot correctly pluralize entity names that end with "s" (e.g. "status", "campus", etc.)

    Here's how I got it and proof.

    • I've created and re-created my original set up several times with the same result.
    • I also tried renaming my table to things like TransStatus and the like - no luck.
    • While I was researching about this I came across the pluralization article by Scott Hanselman where he added pluralization rules for words like sheep and goose. This got me thinking "what if he problem is in the actual name of the entity?"
    • I did some searching about the word status in particular and found this report of the problem on Connect. I rushed to try renaming my entity...
    • Renaming TransactionStatuses to TransactionStates (while even keeping the columns as StatusID) fixed the issue! I can now get List statuses = db.TransactionStates.ToList();
    • I thought the problem was with the particular word status in the name... But after vocally complaining about this to a friend I was suggested that maybe the problem is with the word ending with "s"... So I decided to check it out.
    • I set up another table called Campuses and matching POCO Campus (and updated the edmx). Did the simple test List test = db.Campuses.ToList(); and got the now expected

    Invalid object name 'dbo.Campus'.

    So there you go, the mighty EF cannot handle pluralization of words ending with "s". Hopefully, the next poor bugger hitting the problem will find this question and save him- or herself 3-4 hours of pain and frustration.

提交回复
热议问题