-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Discussion for Announcement 167: Table names now taken from DbSet names (starting in RC2) #4996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Will the [Key] attribute still be respected? |
@PhillipVoyle Did you mean the [Table] attribute? I can't see how this change would impact the naming of Keys. |
@divega Does this affect the naming of objects created by EF in a many to many scenario? |
@PhillipVoyle @miguellira I don't think
EF Core currently doesn't support modeling many-to-many directly without a link entity type. The name of the table for that link entity type will follow the same rules as any other entity type, e.g. the table will be named after the type unless there is a |
@divega Thanks for the quick response. So for the following model: http://docs.efproject.net/en/latest/modeling/relationships.html#many-to-many Since the link entity type is not exposed as a DbSet you would end up with |
@miguellira Yes, I would expect |
@ericwj Yes, I think that synthesizes very well our thinking on this: With this improvement EF Core can leverage naming choices made by the developer instead of being in the business of processing the words and producing plurals for table names in a specific language (we did that in Code First for previous versions of EF and it turned out to be a dead end, especially since any bug fix on the pluralizer becomes a breaking change in the runtime). Design-time/reverse engineering tooling is a different story. We may still add something there: #3060. |
As someone who prefers singular table names but likes plural DbSet names, I disagree with this approach. In my opinion, having the PluralizingTableNameConvention as a default in previous EF versions was a mistake and I would prefer to see that removed as a default while keeping table names the same as class names. Apologies for bikeshedding and maybe I am just out of step with industry practices, but it seems to me far more common that people use singular table names in SQL, while plural DbSet names match better with C# conventions. |
@entitycontext I generally agree with you. The singular/plural table name is a matter of personal preference and the singular form is often chosen to avoid dealing with irregular plurals (e.g. person/people). That said, I think the EF teams wants to avoid that issue by defaulting to a name that you have chosen in your model that should make sense in your database. Fortunately, if it doesn't sit well with you, you can always override it via attributes or fluent definitions. BTW, other technologies (like Ruby/Rails/ActiveRecord) defaults to plural form so "industry practices" will vary by the technologies chosen. 😃 |
@miguellira I agree it really is just a matter of personal preference, but it's annoying to have to fight against the defaults when you don't agree with them. See also: LazyLoadingEnabled and OneToManyCascadeDeleteConvention in EF6, both of which I strongly prefer to have turned off and also arguably should be off by default. Since I want singular table names, I will consider using singular DbSet names as well for new projects in EF Core rather than fighting against the default behavior in the model builder. Honestly, that wouldn't be a terrible thing and might even be better for mentally switching between SQL and LINQ - would just feel weird at first, since I've been pluralizing the DbSet names for so long. |
@entitycontext If you're the only one that will be working on that code do what works for you and keeps you happy. However, if you expect others to see something like: public DbSet<Person> Person { get; set; } when they're used to seeing something like: public DbSet<Person> People { get; set; } and not question your approach... be prepared to do some explaining. 😃 |
There is a two-liner in the announcement however that you could tweak a little to change all table names to the type name and keep the People property name too. |
will there be an option to allow the entity/table to be in a different schema other than dbo ? |
@grahamehorner Maybe I'm missing something but that's always been there. You can use |
@rowanmiller public class Blog Could I have two same structure tables? one table named Blog_One another table named Blog_Two |
@xyting Sounds like a bad design to me. |
@xyting You would need two different context types for that. At a very high level, EF operations are keyed on the CLR type, and so a single CLR type can only map to one table (currently). |
@xyting but why not just add another property-column with the tenant name? (I guess you are trying to solve multi tenancy here) or other solutions like that. |
EF use convention over configuration, which is good. But this option is not quite support any extensibility.
We could use something more suited (in db context or in service configure section).
This is pretty important stuff IMHO. Lets imaging that you have some really big old fashioned enterprise. You wanna adopt new technologies, and business give you green light with .net core. But it only for server side. DB part still need to follow certain standarts, including specific naming convention.
Everyone happy 👍 |
You can do this in We do also have the concept of conventions in EF Core, and providers can already register custom conventions. In fact, getting the table name from DbSet is already a convention that is registered when you use a relational database provider - see TableNameFromDbSetConvention. I'm not sure we want to start exposing granular model building things on context configuration, we already have a special method that deals exclusively with model building |
@rowanmiller yes i can do this in Regards to |
|
@RickyLin good point, here is some more complex code that sets the table name the same way our "get the name from the DbSet property" convention works... var internalBuilder = modelBuilder.GetInfrastructure<InternalModelBuilder>();
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
internalBuilder
.Entity(entity.Name, ConfigurationSource.Convention)
.Relational(ConfigurationSource.Convention)
.ToTable(entity.ClrType.Name);
} |
Forgive me if I'm missing something obvious, but I've ported my asp net app from RC1 to RC2 (using all The default new RC2 MVC new project that VS creates works as expected, and I used it as a reference for porting my app (the .xproj, project.json, and launchSettings.json are pretty much identical). EDIT: Found the issue. The Npgsql database provider seems to not implement, or overrides, the new behaviour. I'll take this issue over to npgsql. In the meantime, is there any code that will force the naming convention to the new style, avoiding a potentially painful migration in the future? EDIT Nevermind X2, found the pull request for the change. The code is slightly too involved to comfortably cram into the |
Are you planning to introduce a pluralization option as was there in EF 6 and earlier versions. This change is quite inconvenient for those of us with large amounts of code built on RC1 |
@bryanjhogan if you need to keep the table names the same as they were before, consider using the workaround detailed at #4996 (comment). We are going to consider using plularization and sigularization for database reverse engineering in the future but we don't want to depend on pluralization to decide the names of tables at runtime. We did that in EF6 and it turned out to be less than ideal, e.g. once we did that, fixing bugs in pluralization would become breaking changes on the runtime. |
We are closing this issue because no further action is planned for this issue. If you still have any issues or questions, please log a new issue with any additional details that you have. |
Where is the comments/discussion link for EF Core: Table names now taken from DbSet names (starting in RC2) Or I'll just drop my thought on this right here:
Kudos @rowanmiller & team for this, it's great!
It always felt like there was some hard to understand reason why this couldn't be done or some data modelling purists didn't want it and truth be told I've not thought of asking for this in EF7, but now finally this is really going to be as intuitive as many people probably thought with me it could be.
Correct me if I'm wrong, I believe EF has always had these complex mapping strategies and pluralization etc while the property of the DbContext is there most of the time, pluralized and spelled correctly for EF to just use.
The text was updated successfully, but these errors were encountered: