0

EF is making some properties non nullable by default in the database, is there some way to over ride this for certain properties ? I tried searching for a data annotation but found nothing.

I am using code first the properties are an int and a datetime

2

1 Answer 1

4

I am assuming that you are using code-first. You are probably getting non-nullable for types that are indeed non-nullable (value types), like int, bool, etc. You need to use the nullable version of these in order to do this:

int? MyColumnThatIsNullable

and NOT this

int MyColumnThatIsNOTNullable

Think about how the code would work and this will make sense to you. If a field is an int and a null is attempted to be passed into the code, then you will either have some sort of error, or a magic (default) value. This will not properly represent the value in the database. So, if you use the nullable version, then the null in the database can be represented for what it truly is, a null.

Sign up to request clarification or add additional context in comments.

2 Comments

Hah! now that you mention it that makes perfect sense, and I cant figure out how I missed it. Thanks
@Roge No problem, I added an explanation below my original answer (although you said you got it :)). As usual accepted answers (once the 15 min grace period is up) and upvotes are appreciated :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.