Why are we (still) writing our SchemaBuilder.CreateTable statements by hand?

Topics: Core, General, Writing modules
Feb 5, 2014 at 4:49 PM
After messing up a letter in a column name for a new record table, I was wondering why in the world I need to spell out the mapping of record properties to table columns by hand.

Take this example:
SchemaBuilder.CreateTable("ProductPartRecord",
    table => table.ContentPartRecord()
                .Column<string>("ProductName")
                .Column<string>("Description", col => col.WithLength(4000))
                .Column<string>("CancellationTerms", col => col.WithLength(1000))
                .Column<int>("ValidityPeriod")
                .Column<decimal>("Price"));
My record looks like this:
public class ProductPartRecord : ContentPartRecord {
    public virtual string ProductName { get; set; }
    [StringLength(4000)]
    public virtual string Description { get; set; }
    [StringLength(1000)]
    public virtual string CancellationTerms { get; set; }
    public virtual int ValidityPeriod { get; set; }
    public virtual decimal Price { get; set; }
}
There's nothing here that couldn't be easily automated. I understand that for advanced scenarios one might want/have to adjust the mapping here and there but in most situations I could imagine using something like the following should be all that's needed:
SchemaBuilder.CreateTable<ProductPartRecord>();
What do you think?
Feb 5, 2014 at 6:33 PM
Agreed, it would be awesome, if there was a GUI that will generate the models and migrations files for our custom content type modules. It wouldn't be a complicated build.
Coordinator
Feb 6, 2014 at 8:29 PM
It used to be automatic, before we had migrations. This needs to be explicit, it was a very deliberate decision. There are code gen options out there however that can help.
Feb 7, 2014 at 6:50 AM
Thanks Bertrand, I am not aware of the code gen options, can you please point us to the options available?
Developer
Feb 7, 2014 at 11:49 AM
See: http://docs.orchardproject.net/Documentation/Command-line-scaffolding We could have helpers for the SchemaBuilder to make such method calls strongly-types (somehow like AddColumn(record => record.MyProperty)). However we have to keep in mind that e.g. simply renaming the property won't rename it in the DB too... So still there is a migration step required.
Feb 7, 2014 at 12:46 PM
It would've been great if you can setup the content type in admin and you run a command line utility to reverse engineer it into a schema and migration file, so you can reuse the content type across different projects. Just an idea.
Developer
Feb 7, 2014 at 12:59 PM
Well you can export then import is, so you don't even have to write code.
Feb 7, 2014 at 2:16 PM
I guess thats right, only bad thing is that you don't have the luxury of a typed part, which is nice sometimes.