Kalvis
Kalvis

Reputation: 635

How to make DateTime field nullable in Local SQL db

I'm working on my ASP.NET MVC 4 project and I'm facing a problem caused with my local data base. I have a few tables in there, in one of them I have "DateTime" type field. I want that field not to be required, so I have already set "Allow Nulls" option in table's designer view as "true". I also added some data in that table, in fact I didn't enter values in that "DateTime" field.

When I run my app and try to open the view where the data from my table should appear I get an error: "The 'DateFrom' property on 'Person' could not be set to a 'null' value. You must set this property to a non-null value of type 'DateTime'"

Could somebody help me with this issue?

PS: I'm using VisualStudio 2012 Ultimate and programming in c#

I created DB with code-first method:

Here is the class of an Person:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace PersonuRegistrs.Models

    {
        public class Persona
        {
            [Key]
            [Required]
            public int PersonaId { get; set; }

            public FiziskaPersona FiziskaPersona { get; set; }

            public JuridiskaPersona JuridiskaPersona { get; set; }

            [DisplayFormat(NullDisplayText="-")]
            public string IdentifikacijasNumurs { get; set; }

            public bool IrPrezidents { get; set; }

            public double Ien_Likme { get; set; }

            [DisplayFormat(NullDisplayText="-")]
            public DateTime IenSpekaNo { get; set; }

            [DisplayFormat(NullDisplayText = "-")]
            public DateTime IenSpekaLidz { get; set; }

            public bool IrMaksatnespejiga { get; set; }

            public Adrese Adrese { get; set; }

            public bool SanemtApkartparakstu { get; set; }

            [Required]
            public IerakstaStatuss IerakstaStatuss { get; set; }

            public List<Strukturvieniba> PersonasStrukturvienibas { get; set; }
        }
    }

And here is the designer view of that table: enter image description here

enter image description here

Upvotes: 1

Views: 154

Answers (1)

Dan Bracuk
Dan Bracuk

Reputation: 20804

It's not a database problem. In your Person class, change the DateFrom property from DateTime to DateTime?. That will make it nullable.

Upvotes: 1

Related Questions