Want to create this table on application start if it does not exist.
Code:
public class Database : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        var db = applicationContext.DatabaseContext.Database;
        //Cant add this table due to the ENUM
        if (!db.TableExist("FormData"))
        {
            db.CreateTable<FormData>(false);
        }
    }
}
Model:
[PrimaryKey("Id")]
public class FormData
{
    [PrimaryKeyColumn(AutoIncrement = true, IdentitySeed = 1)]
    public int Id { get; set; }
    [NullSetting(NullSetting = NullSettings.NotNull)]
    public FormType Type { get; set; }
    [NullSetting(NullSetting = NullSettings.NotNull)]
    public string Data { get; set; }
    [NullSetting(NullSetting = NullSettings.NotNull)]
    public DateTime Date { get; set; }
}
Error message:
[InvalidOperationException: Sequence contains no matching element] System.Linq.Enumerable.First(IEnumerable
1 source, Func2 predicate) +415 Umbraco.Core.Persistence.SqlSyntax.SqlSyntaxProviderBase1.FormatType(ColumnDefinition column) +1225 Umbraco.Core.Persistence.SqlSyntax.SqlSyntaxProviderBase1.Format(ColumnDefinition column) +155 Umbraco.Core.Persistence.SqlSyntax.SqlSyntaxProviderBase1.Format(IEnumerable1 columns) +144 Umbraco.Core.Persistence.SqlSyntax.SqlSyntaxProviderBase`1.Format(TableDefinition table) +131 Umbraco.Core.Persistence.PetaPocoExtensions.CreateTable(Database db, Boolean overwrite, Type modelType) +161 Umbraco.Core.Persistence.PetaPocoExtensions.CreateTable(Database db, Boolean overwrite) +121
Looking at the error I dont think there is a solution to this without updating the core but here is to hoping you guys can help
Working on the answer that @Ryios gave, I think that something like this is good:
/// <summary>
/// Don't use this to get or set.
/// This must however be kept as public or db.CreateTable()
/// will not insert this field into the database.
/// </summary>
[NullSetting(NullSetting = NullSettings.NotNull)]
[Column("type")]
public int _type { get; set; }
/// <summary>
/// This field is ignored by db.CreateTable().
/// </summary>
[Ignore]
public FormType Type
{
    get
    {
        return (FormType)_type;
    }
    set
    {
        _type = (int)value;
    }
}
In the code, Type should be used rather than _type so that the enum can be benefitted from. _type is only present as the field that is inserted into the database table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With