efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Alphabetic column order table generation breaks Entity Framework Code-First principle

Open giddev opened this issue 11 years ago • 12 comments

Entity Framework Beta 8 Code-first creates SQL Server table columns from a POCO class not in the order they appear in, as was the case in EF6 but in in alphabetic order. (apart the primary key)

This breaks the fundamental principle of Code-First.

The order of the SQL Server table columns should be the same order the properties appear in the POCO class.

giddev avatar Nov 17 '15 09:11 giddev

This one is interesting... in EF6.x you theoretically get the order things are defined in the class but in actual fact it depends on what processor you run on. We saw plenty of cases where the model would change between x86/x64... this causes all sorts of headaches because it can have very subtle effects on conventions etc. and result in a different model between processors.

The other data point is that column ordering only works for the initial table creation. After that, columns are always added to the end of the table.

If you want to change the ordering though... you can just re-arrange things in the generated migration. That is explicit, and columns will be created in the order they are listed in the create table call. And we know it won't change, since the API calls are explicitly listed in code.

rowanmiller avatar Nov 20 '15 18:11 rowanmiller

Thank you very much for all your great work Rowan! We are anxiously waiting for a fully baked version.

The Framework should work for me, not the other way around.

  • Developer should be able to control the generated code and overwrite column ordering by the generator.
  • Generator should warn about the possible side effects and provide methods to test them.
  • Can the Entity Framework team or Rowan Miller provide an extension that disables the annoying alphabetical column ordering by the code generator?

Are we ordering because we cannot fix an x86/x64 issue? Let's fix it, please :-)

IvanFarkas avatar Nov 22 '15 19:11 IvanFarkas

@IvanFarkas you can change the order of columns in the database by re-arranging things in the migration. We have no way of getting properties in the order they are defined in the class. You usually get them in that order on x86 on Full .NET... and often on x64 too. But as we support more platforms etc. these guarantees just get less and less.

Another place where something like a column order attribute falls down is if you change the order... since this is not supported by most relational database. Or if you try to add a new property and specify the column should be somewhere in the middle of existing columns... also not supported. Of course you can kind of fake it by doing a table re-build... but that's not a realistic solution for a decent size production database.

To us, it seems the right time to specify column order is when the table is created (in the migration).

rowanmiller avatar Nov 23 '15 18:11 rowanmiller

@rowanmiller _Thank you very much for the thorough explanation._ It seems to me that EF7 is a paradigm shift where we gain a lot and lose some. It's nice to know that db strategies and infrastructure works the same way on a massive Azure cluster and on Raspberry Pi 2 Model B. On the other hand, given the explained limitations, can we still do something only for the initial code generation? All new columns will be appended.

  1. Use [Order(n)] attributes.
  2. Code post processor to order columns as they are in POCO.

Thank you again for your great work.

IvanFarkas avatar Nov 24 '15 02:11 IvanFarkas

@rowanmiller Great progress with EF Core 1.1. I love your EF Core 1.1 articles:

Unfortunately could not get the Pluralization to work. I'd love to :-)

Using VS 2017 RC4 26206.0

Install-Package Inflector returns this error:

Install-Package : Package Inflector 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Inflector 1.0.0 supports:

  • net35 (.NETFramework,Version=v3.5)
  • net40 (.NETFramework,Version=v4.0)
  • net45 (.NETFramework,Version=v4.5)
  • winrt45 (WinRT,Version=v4.5)

IvanFarkas avatar Feb 16 '17 03:02 IvanFarkas

@rowanmiller How can I generate a base entity type with Reverse Engineering? Desired Code: public partial class Blog : BaseEntity

IvanFarkas avatar Feb 16 '17 05:02 IvanFarkas

@IvanFarkas there isn't native support for this. You could look at extending the internal services of reverse engineer to achieve this, but you'd be digging into internal APIs that may change between releases.

rowanmiller avatar Feb 16 '17 17:02 rowanmiller

@IvanFarkas Inflector is just used to do the pluralization, but looks like it doesn't support .NET Core. You could easily swap it out for any other pluralization service - it is only used in two places in the code.

rowanmiller avatar Feb 16 '17 17:02 rowanmiller

@rowanmiller Thx for the quick replies. One more issue. CustomRelationalScaffoldingModelFactory.GetEntityTypeName(TableModel table) never gets called if I run the command line app. It runs if I run Scaffold-DbContext from the Package Manager Console for my data layer project.

IvanFarkas avatar Feb 22 '17 17:02 IvanFarkas

@IvanFarkas can you elaborate on what you mean by "never gets called if I run the command line app."?

BTW I just updated the post with a link to the source code file for Inflector. The package won't install for .NET Core, but the code compiles just fine if you drop it in (and it's just one file).

rowanmiller avatar Feb 22 '17 23:02 rowanmiller

@rowanmiller Sorry Rowan. My fault. I integrated the Pluralization Code into the Run Reverse Engineer console app. Running the console app GetEntityTypeName() is not called. Integrated Inflector. Works great. Thx.

IvanFarkas avatar Feb 23 '17 00:02 IvanFarkas

The console app is creating the service collection manually, so the code would look like this...

            // Add base services for scaffolding
            var serviceCollection = new ServiceCollection()
                .AddScaffolding()
                .AddLogging();

            // Replace customized services
            services.AddSingleton<IScaffoldingModelFactory, CustomRelationalScaffoldingModelFactory>();
            services.AddSingleton<DbContextWriter, CustomDbContextWriter>();
            services.AddSingleton<ConfigurationFactory, CustomConfigurationFactory>();

rowanmiller avatar Feb 23 '17 23:02 rowanmiller