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

前端 未结 2 577
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 11:24

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

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

    pu         


        
2条回答
  •  情歌与酒
    2020-12-14 12:26

    You will need a mapping rule like this:

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

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

提交回复
热议问题