You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Names and default names for keys, constraints and indices #12837
While working on #12821, I noticed some that may merit some discussion.
When scaffolding, RelationalScaffoldingModelFactory checks the database name for keys, constraints and indices, comparing them against the default name EF Core would assign to this index. The idea is presumably to avoid useless scaffolding HasName() calls which would bloat OnModelCreating().
Now, at least in the PostgreSQL case (but I think also the others), a name is always returned for keys/constraints/indices. PostgreSQL has its own defaults: primary keys that haven't been explicitly named are called {table}_pkey, etc. This means that for a typical database that has been created without specifying names, HasName() will be scaffolded everywhere because the PostgreSQL defaults aren't the same as the EF Core defaults (PK_{table}).
There seem to be two options:
Current behavior: always take the name reported by the database. This bloats OnModelCreating() with HasName() calls on every single key, constraint and index. But it does have the advantage of round-trippability (exact names are preserved from the scaffolded database).
Alternative behavior: have the database model factories omit names when those names are the database defaults, leading to null names and less HasName() everywhere. Since EF Core must specify names when creating keys/indices, it would see nullls and default create indexes and keys with its own defaults.
Basically it's a choice between round-trippability (current behavior) and a leaner OnModelCreating(). I'm not sure to what extent the former is an actual goal.
@roji If I remember rightly, I think the idea was that the names generated by EF by default for PostgreSQL would match the "most common convention" for names created in that database. It sounds like for PostgreSQL this would be to match the names that the database will use if no name is provided.
However, it would be a breaking change to start generating the names in this way now, in that a new migration would be generated to rename all the constraints to their new values. We will discuss in triage what options there might be here.
I see... If I read the code right, that isn't possible currently, is it? I mean, the default name generation seems to be static methods which the providers cannot override etc.
Regardless yeah, it would be a breaking change but it definitely seems like the most correct way to deal with this... Maybe for 3.0?
@roji I looked into this code and we effectively broke the extensiblity model here when we simplified the service provider and model building process in 2.0--dumping "one model to rule them all". This is definitely something we should look at for 3.0.
We also discussed this in triage, and I believe there is a desire to be able to reverse engineer in two ways:
To a model that contains all information needed to then continue managing that database with Migrations. This mode would require thinks like constraint names to be included.
To a model that will only be used by the EF runtime and therefore can be smaller because it doesn't include things that EF doesn't care about.
@bricelam is going to link issues related to this. I don't think we have decided exactly how APIs, switches, etc will look, nor what the default will be.
I only care about: "To a model that will only be used by the EF runtime and therefore can be smaller because it doesn't include things that EF doesn't care about."
I guess this is the right discussion to also think about round-trippability, which is related but not exactly identical to the first reverse-engineering "mode".
Personally having two different reverse-engineering modes (command-line switch?) seems a bit complicated too me, it will probably be difficult for users to understand etc. My gut feeling is that a bit of bloat in the model (stuff like constraint and index names) isn't that bad, or at least not bad enough to warrant a whole new mode. After all, if users don't care about that stuff they can just manually delete it from the model (although if you're working on "iterative" reverse-engineering to continue to update models that might get tricky as false changes get detected).
With #10890 we could make the default reverse engineering experience not include constrint names. Part of #2167 is to add a gesture to Scaffold-Database to indicate you want to start using Migrations with it; this would signal that we should reverse engineer things like constraint names. The update model from database feature probably doesn't need the names since it doesn't need to rename anything (it just re-scaffolds everything) and a structural comparison is probably sufficient.
I'm not sure if this is the right place to request this feature. It seems related.
KeyDiscoveryConvention.DiscoverKeyProperties(IConventionEntityType entityType, IEnumerable<IConventionProperty> candidateProperties) is currently static. It would be much simpler to change the key name convention if that method could be overridden. Currently we can only override DiscoverKeyProperties(IConventionEntityType entityType), which calls the two-argument version and does a bunch of other things that the override would have to duplicate.
I thought this change was going to be in .NET 9 as part of #28866 but either I misread the pending changes or it was backed out.
Activity
ajcvickers commentedon Jul 30, 2018
@roji If I remember rightly, I think the idea was that the names generated by EF by default for PostgreSQL would match the "most common convention" for names created in that database. It sounds like for PostgreSQL this would be to match the names that the database will use if no name is provided.
However, it would be a breaking change to start generating the names in this way now, in that a new migration would be generated to rename all the constraints to their new values. We will discuss in triage what options there might be here.
ajcvickers commentedon Jul 30, 2018
@bricelam to link related issues.
roji commentedon Jul 30, 2018
I see... If I read the code right, that isn't possible currently, is it? I mean, the default name generation seems to be static methods which the providers cannot override etc.
Regardless yeah, it would be a breaking change but it definitely seems like the most correct way to deal with this... Maybe for 3.0?
ajcvickers commentedon Jul 31, 2018
@roji I looked into this code and we effectively broke the extensiblity model here when we simplified the service provider and model building process in 2.0--dumping "one model to rule them all". This is definitely something we should look at for 3.0.
We also discussed this in triage, and I believe there is a desire to be able to reverse engineer in two ways:
@bricelam is going to link issues related to this. I don't think we have decided exactly how APIs, switches, etc will look, nor what the default will be.
ErikEJ commentedon Jul 31, 2018
I only care about: "To a model that will only be used by the EF runtime and therefore can be smaller because it doesn't include things that EF doesn't care about."
roji commentedon Jul 31, 2018
I guess this is the right discussion to also think about round-trippability, which is related but not exactly identical to the first reverse-engineering "mode".
Personally having two different reverse-engineering modes (command-line switch?) seems a bit complicated too me, it will probably be difficult for users to understand etc. My gut feeling is that a bit of bloat in the model (stuff like constraint and index names) isn't that bad, or at least not bad enough to warrant a whole new mode. After all, if users don't care about that stuff they can just manually delete it from the model (although if you're working on "iterative" reverse-engineering to continue to update models that might get tricky as false changes get detected).
Just my two cents...
bricelam commentedon Jul 31, 2018
Some notes:
With #10890 we could make the default reverse engineering experience not include constrint names. Part of #2167 is to add a gesture to
Scaffold-Databaseto indicate you want to start using Migrations with it; this would signal that we should reverse engineer things like constraint names. The update model from database feature probably doesn't need the names since it doesn't need to rename anything (it just re-scaffolds everything) and a structural comparison is probably sufficient.ajcvickers commentedon Aug 1, 2018
Triage: keeping this open for 3.0 to look at:
Other aspects discussed here are tracked by #10890, #2167.
14 remaining items
AndriySvyryd commentedon May 10, 2019
This could be handled by default value conventions
kjkrum commentedon Mar 7, 2025
I'm not sure if this is the right place to request this feature. It seems related.
KeyDiscoveryConvention.DiscoverKeyProperties(IConventionEntityType entityType, IEnumerable<IConventionProperty> candidateProperties)is currently static. It would be much simpler to change the key name convention if that method could be overridden. Currently we can only overrideDiscoverKeyProperties(IConventionEntityType entityType), which calls the two-argument version and does a bunch of other things that the override would have to duplicate.I thought this change was going to be in .NET 9 as part of #28866 but either I misread the pending changes or it was backed out.