0

I created my database with entity framework - code first.

public class Customer:BaseEntity
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string VerPassword { get; set; }
    public string SecurityQuestion { get; set; }
    public string SecurityAnswer { get; set; }
    public int RolId { get; set; }
    public int QR { get; set; }
}

I have a working registration page but know I added QR part which is integer. I do not want to user give any input for this and I want it to be NULL in the database.

I set all of the datas to database with this design pattern

_customerService.Add(model);

I tried to set null to QR column like this but it does not work

int? value = 0;

if (value == 0)
{
    value = null;
}
model.QR = value;

The error is "Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)"

What can I do to set null value to QR column

1
  • 1
    Change the type of QR to int?. Otherwise you'll end up with QR being not null in your DB. Commented Jan 7, 2023 at 0:56

1 Answer 1

3

What can I do to set null value to QR column

int does not allow null's, so you need to change QR type to be a nullable int (i.e. int? or Nullable<int>):

public class Customer:BaseEntity
{
    // ...
    public int? QR { get; set; }
}

Read more:

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

Comments

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.