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 8448035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:19:18+00:00 2026-06-10T10:19:18+00:00

I have a database first design. I want to have default values for my

  • 0

I have a database first design. I want to have default values for my model so that a user can just leave an editor textbox blank for one of my nullable fields. EF doesn’t seem to want to just throw null into the database, so I was hoping I could set default values to null for my nullable attributes.

Ie I want to insert null into my database for an attribute if the textbox is left blank when submitted.

I’ve read you can add a contructor to a partial class to do what I want. So you would basically have:

public partial class MyClass{
    public MyClass()
    {
        field1 = null; //this would be the default value for field1
    }
}

The only problem is the autogenerated partial class for the model I’m working on already has a constructor, so I can’t add a constructor to a different (permanent) partial class. I don’t want to update the autogenerated partial class because it will just be overwritten when I update my edmx from the database.

  • 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-10T10:19:20+00:00Added an answer on June 10, 2026 at 10:19 am

    The default value of the string should already be null, so I think you are taking the wrong path.

    The problem is a textbox will always be passed with a form post, even if it is blank, which will result in an empty string in your controller rather than a null value, even if you find a way to explicitly set your string to null in your initial class.

    Instead, check the value of the string in your controller after the form is posted back, and if it is empty, set it to null.

    if (String.IsNullOrEmpty(formModel.MyString)) formModel.MyString = null;
    

    Either that, or create a default model binder to check the value of the field during model binding, and not set it if it is empty.

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

Sidebar

Related Questions

I have a database that is accessed by two applications. The first is an
I have a database that is currently sharded. This is the first time that
i'm just starting to learn about database design. For my first project I made
My question is related to database design and also how to model that design
I have to adapt a PHP-script that is connected to a extremely messy database-design.
I want to design a fast database schema which can handle sorting and filtering
i have 3 Tables is my SQL Database: First Table: Room: ID <pk>, Roomname
I'm new to Entity Framework and am using Database First. I have inherited a
I have a database framework where I have two tables. The first table has
So I have a database with a few tables. The first table contains the

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.