Table naming issue with POCO's and Database first
-
Saturday, May 05, 2012 11:49 PM
I have a database already created and I want to use my POCO's as entities instead of the EDMX. I've created a few small console app tests against my database and I always get exceptions complaining that a database table doesn't exist. It's looking for a table with a pluralized name and all my tables are singular names.
This has always worked when I use the EDMX designer, but when I try using either ObjectContext, or DbContext with my POCO's, it's always looking for a plural version of the table name.
Is this some convention built into EF when using POCO's? Is there a way of getting around this so I don't have to re-name all my tables?
In other words, I'd like to keep my singularly named tables, while using EF with my POCO's.
BTW, my POCO classes match my tables exactly, in names and plurality (i.e. - they are all singular).
Any help is appreciated.
Thanks.
All Replies
-
Monday, May 07, 2012 2:26 AMModerator
Hi kwilder,
Welcome to MSDN Forum.
When you create a edmx file, at the step of "Choose Your Database Objects", you can find a selection of named "Pluralize or singularize generated object names", uncheck it and click finish. The Entity Sets' names will be singular. After that, you can find the ObjectSet name is singular in the .context.tt file.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
-
Monday, May 07, 2012 5:28 AM
Hi kwilder,
Welcome to MSDN Forum.
When you create a edmx file, at the step of "Choose Your Database Objects", you can find a selection of named "Pluralize or singularize generated object names", uncheck it and click finish. The Entity Sets' names will be singular. After that, you can find the ObjectSet name is singular in the .context.tt file.
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Allen,
Sorry you misunderstood my question. I want to use EF WITHOUT using the EDMX designer, like with DbContext and have it point to my existing database, but I need it to understand and accept my table naming convention and not create it's own.
Is this possible?
Thanks.
-
Monday, May 07, 2012 5:42 AMModerator
Hi kwilder,
Glad to see you again! Could you please post your test project code here?
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
- Edited by Allen Li - AI3Microsoft Contingent Staff, Moderator Monday, May 07, 2012 8:51 AM
-
Monday, May 07, 2012 5:14 PM
Hi kwilder,
Glad to see you again! Could you please post your test project code here?
Best Regards
Allen Li [MSFT]
MSDN Community Support | Feedback to us
Allen,
Thank you for your help, I actually found the answer to my problem.
I used the OnModelCreating method to handle the table pluralizing issue I was having.
public class Database : DbContext { #region Private Variables private IDbSet<Blog> _blogs; private IDbSet<BlogCategory> _blogCategories; private IDbSet<BlogPost> _blogPosts; private IDbSet<BlogComment> _blogComments; #endregion #region ctors public Database(string connectionString) : base(connectionString) { } #endregion #region Public Properties public IDbSet<Blog> Blogs { get { return _blogs ?? (_blogs = Set<Blog>()); } } public IDbSet<BlogCategory> Categories { get { return _blogCategories ?? (_blogCategories = Set<BlogCategory>()); } } public IDbSet<BlogPost> Posts { get { return _blogPosts ?? (_blogPosts = Set<BlogPost>()); } } public IDbSet<BlogComment> Comments { get { return _blogComments ?? (_blogComments = Set<BlogComment>()); } } #endregion #region Overrides protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } #endregion }I found the answer in this series, which is excellent so far.
I found the documentation on DbContext (for EF 5 beta), but the examples were spotty at best and there was no documentation for this particular solution for removing the table pluralization. Do you know if some more in-depth documentation exists somewhere?
Thanks.
- Marked As Answer by Allen Li - AI3Microsoft Contingent Staff, Moderator Monday, May 14, 2012 2:45 AM
-
Wednesday, May 09, 2012 1:52 AMModerator
Hi kwilder,
I'm glad to hear that you have solved the issue. Here's a series of DBContext introduction from ADO.NET Team Blog, but doesn't specific to EF5. EF5 has not released now, if there're some update about DBContext for EF5, I think ADO.NET Team Blog will publish some articles to introduce in the future. So please refer to this series before any update for it.
Allen Li [MSFT]
MSDN Community Support | Feedback to us

