How to allow null values on Foreign keys using EF?

12,910

Solution 1

How to allow null values on Foreign keys using EF?

Your database table should allow nulls as well if you are planning to take the following approach:

change

public int idUser; { get; set; }
[Required]
public string UserName { get; set; }
public virtual Computer Computer { get; set; }

To

public int idUser; { get; set; }
[Required]
public string UserName { get; set; }
public virtual Computer Computer{ get; set; }

And change

[Key, ForeignKey("User")] 
public int idUser { get; set; }
public string ComputerName { get; set; }
public virtual User User { get; set; }

To

[Key, ForeignKey("User")] 
public int? idUser { get; set; }
public string ComputerName { get; set; }
public virtual User User { get; set; }

The ? character, makes a field nullable. Make sure it is nullable in your database as well.

Solution 2

I assume you are using code first. If that is the case then you override OnModelCreating in the context class and add the following line

modelBuilder.Entity<Computer>().HasOptional(c => c.User);

================ Ok, you changed your post after I posted my answer.

You can use

int? idUser

for your user ID, this will let EF know that you are allowing a nullable relationship.

Solution 3

You can make use of ? keyword. It makes your type a Nullable type.

For example int doesn't allow nulls normally.

But after appending a ? to int you can assign null to it.

int? iCanHaveNull = null;
Share:
12,910
Lucie kulza
Author by

Lucie kulza

Updated on June 04, 2022

Comments

  • Lucie kulza
    Lucie kulza almost 2 years

    How to allow null values on Foreign keys using EF?

    public class User
    {
        public User() 
        { 
        }
    
        public int idUser; { get; set; }
        [Required]
        public string UserName { get; set; }
        public virtual Computer Computer{ get; set; }
    }
    
    public class Computer
    {
        public Computer() 
        {
        }
        [Key, ForeignKey("User")]
        public int idUser{ get; set; }
        public string ComputerName {get;set;}
        public virtual User User { get; set; }
    }
    

    Running this I'm getting not null on the foreign key.

  • Lucie kulza
    Lucie kulza about 10 years
    what about string datatype?
  • Max
    Max about 10 years
    @Luciekulza A string is a reference type and nullable by default.
  • Lucie kulza
    Lucie kulza about 10 years
    Need I to write the ? character on User? and int? the same time?
  • Max
    Max about 10 years
    Only for User and Computer, since those are the foreign keys you want to be nullable
  • Keith
    Keith about 10 years
    What the @#!@#$? the ? character is to allow nullable types for basic data (int, double, bool, etc). A Class by definition is already a nullable type. This answer makes no sense.
  • Max
    Max about 10 years
    @Keith Database should be equal to code, if he decides to make this foreign key nullable at the database, same should be done in code for cosistency.
  • Keith
    Keith about 10 years
    That's true, but your answer is still incorrect. you CANNOT use ? on an already nullable type. The ? MUST be on the int? idUser property. If you don't believe me, try your solution. It will NOT compile.
  • oatsoda
    oatsoda almost 7 years
    This answer is just plain wrong. As per @Keith comment, unless User is a struct then the code won't even compile.
  • Sras
    Sras almost 4 years
    only for int, but what about string or guid