Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:09:34+00:00 2026-06-14T21:09:34+00:00

I have a DateTime field that I want to set to a default value

  • 0

I have a DateTime field that I want to set to a default value whenever a record is entered into that table (it is basically a DateCreated field). This is my code

public partial class User
    {
        public User()
        {
            this.DateCreated = DateTime.Now; //set default value
            Roles = new HashSet<Role>();
        }

        public ICollection<Role> Roles { get; set; } //many to many relationship

        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string City { get; set; }

        //foreign key
        public int CountryId { get; set; }
        //navigation properties
        public virtual Country Country { get; set; }

        //foreign key
        public int LanguageId { get; set; }
        //navigation properties
        public virtual Language Language { get; set; }

        public string EmailAddress { get; set; }
        public long FacebookId { get; set; }
        public DateTime DateCreated { get; set; }
    } }

    public class UserConfiguration : EntityTypeConfiguration<User>
    {
        public UserConfiguration()
        {
            ToTable("Users", schemaName: "Main");

            HasKey(s => s.UserId);

            Property(p => p.FirstName)
                .IsRequired().HasMaxLength(50); //translates to non-nullable     

            Property(p => p.Surname)
                .IsRequired().HasMaxLength(50);

            Property(p => p.Username)
                .IsRequired().HasMaxLength(20);

            Property(p => p.Password)
                .IsRequired().HasMaxLength(20);

            Property(p => p.City)
                .IsOptional().HasMaxLength(50); //not required

            Property(p => p.LanguageId)
                .IsRequired();

            Property(p => p.EmailAddress)
                .IsRequired().HasMaxLength(50);

            Property(p => p.FacebookId)
                .IsOptional();

            Property(p => p.DateCreated) 
                .IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            //attribute ensures that DateCreated only gets saved to the database when you are adding a 
            //row, not when you are saving
        }
    }

    context.Users.Add(new User
                {
                    FirstName = "Test",
                    Surname = "User",
                    Username = "testuser72",
                    Password = "testuser72",
                    City = "London",
                    CountryId = context.Countries.Single(d => d.CountryName == "United Kingdom").CountryId,
                    LanguageId = context.Languages.Single(d => d.LanguageName == "English").LanguageId,
                    EmailAddress = "testuser@yahoo.co.uk",
                    Roles = new Role[] { context.Roles.Single(r => r.RoleName == "Content Editor"), context.Roles.Single(s => s.RoleName == "User") }
                });

Why when I try to add a user using the code above why do I get the error ‘Cannot insert the value NULL into column ‘DateCreated’, table ‘Main.Users’;
column does not allow nulls. INSERT fails.’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:09:35+00:00Added an answer on June 14, 2026 at 9:09 pm

    With the option “HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)” you say you want it to be an auto incremended column. This is not what you want. You want Computed, which says the value is automatically filled by the database, and not by you.

    Just changing this option does not solve your problem. By setting it to Computed, you say you don’t want it to take your own values, so settig it in the constructor is pointless. You have to set a default value at the database level to “GETDATE()”. When you use migrations, add the string to the defaultValueSql parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with CreateDate datetime field default(getdate()) that does not have any
I have a datetime field like that. <field name=rel_date type=date indexed=true stored=true default=NOW multiValued=false/>
I have a DATETIME field in a table where I set up as '0'
I have a trigger that sets a datetime field in a table row when
I have a table with a datetime field. I want to retrieve a result
If I have a datetime field (like Birthdate) in Sql Server, and I want
I have a DateTime object I want to compare against an sql datetime field
I'm using SQL Server 2005 express I have a datetime field in a table
I have a MySQL table called items that contains thousands of records. Each record
I have the simplest model with a single Date field that I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.