[Solved] Creating custom tables
# help-with-umbraco
h
Can anyone tell me how to set a table column to allow nulls when doing a migration. I have the following model
Copy code
csharp
    [TableName(TableConstants.Questions.TableName)]
    [ExplicitColumns]
    [PrimaryKey("Id", AutoIncrement=true)]
    public class Question
    {
        [Column("Id")]
        [PrimaryKeyColumn(AutoIncrement = true)]
        public int Id { get; set; }

        [Column("Name")]
        public string Name { get; set; }

        [Column("Responses")]
        public int ResponseCount { get; set; }

        [Column("StartDate")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public DateTime? StartDate { get; set; }

        [Column("EndDate")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public DateTime? EndDate { get; set; }

        [Column("CreatedDate")]
        public DateTime CreatedDate { get; set; }

        [Ignore]
        public IEnumerable<Answer> Answers { get; set; }
        [Ignore]
        public IEnumerable<Response> Responses { get; set; }
    }
I am executing
Create.Table<Question>().Do();
but it does not set the datetime fields to allow nulls
j
I always just add [NullSetting], but not sure I've combined it with datetime
h
worth a try 😄
j
Hmm found this in a project, seemingly works:
Copy code
[NullSetting]
public DateTime? ClosingDate { get; set; }
Don't know if making the type nullable has any effect
h
They are nullable
Copy code
csharp
        [Column("EndDate")]
        [NullSetting(NullSetting = NullSettings.Null)]
        public DateTime? EndDate { get; set; }
[NullSetting] worked, thanks @Jemayn
a
Didnt you already have this in your example?
h
no, I had
[NullSetting(NullSetting = NullSettings.Null)]
which didn't work, just using` [NullSetting]` does
a
Oooh good to know, thanks!
n
Hello, Sorry to jump in .New to C# and umbraco Just wanna ask how to setup relationship with tables I found this in the above [Ignore] public IEnumerable Answers { get; set; } Is that another table that related to Question table ?
h
yes
n
thank you for you response. what does [Ignore] do ?
h
The Ignore is to prevent it from creating a column called answers in the question table when the table is created.
n
oh ok, Thank you .
4 Views