Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Entity Framework Code First and nullable foreign key properties to work?

I'm trying to create a simple entity framework code first application. I have these classes:

public class User
{
    public int UserId { get; set; }

    public string Username { get; set; }

    public virtual ActivationTicket ActivationTicket { get; set; }
}

public class ActivationTicket
{
    public int ActivationTicketId { get; set; }

    public virtual User User { get; set; }

    public string Ticket { get; set; }
}

When I try to create a new user and save it to the database (a user without a ActivationTicket that is) I receive an exception

The INSERT statement conflicted with the FOREIGN KEY constraint "ActivationTicket_User". The conflict occurred in database "Test", table "dbo.ActivatioTickets", column 'ActivationTicketId'. The statement has been terminated.

I assume EF treats the mapping between User and ActivationTicket as 1-1 but it should be 1-0..1

What do I have to do to get this to work?

like image 875
b3n Avatar asked Mar 04 '11 04:03

b3n


People also ask

How do you define a foreign key in Entity Framework Code First?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship.

Can a foreign key be null EF core?

A foreign key is NULLABLE by DEFAULT in code first asp.net mvc - 5 entity framework. If we want to make it non nullable. we need to either use fluent api if not then decorate with "Required" attribute.

Does Entity Framework support foreign keys?

When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.


2 Answers

@b3n It should be enough to do this, at least with VS 2013:

public class User
{
   public int UserId { get; set; }

   public string Username { get; set; }

   public int? ActivationTicketId { get; set;}

   public virtual ActivationTicket ActivationTicket { get; set; }
}

This is the important part:

public int? ActivationTicketId { get; set;}

This will specify the foreignkey in your "User" table for the ActivasionTicket and define that it's optional.

credits go to: http://forums.asp.net/t/1948351.aspx?Change+Foreign+Key+to+nullable+using+Code+First#5554732

I had the same problem and it instantly worked for me.

Also as a note i marked all my primary keys with the data annotation "[Key]". This might be necessary in order to make this work.

like image 128
Leo Gerber Avatar answered Oct 03 '22 06:10

Leo Gerber


You will need a mapping rule like this:

modelBuilder
    .Entity<User>()
    .HasOptional<ActivationTicket>(u => u.ActivationTicket)
    .WithOptionalPrincipal();

This will give you an ActivationTickets table with a UserId that is nullable.

like image 44
anon Avatar answered Oct 03 '22 04:10

anon